Minimum Last-Element Relocations
Problem statement
The arrays current and desired contain the same multiset of values. In one operation, remove the last element of the current array and insert it at any position.
Return the minimum number of operations needed to transform current into desired.
Function
minimumRelocations(current: int[], desired: int[]) → intExamples
Example 1
current = [2,1,3,5,4]desired = [2,4,1,5,3]return = 2The unmatched suffix is exactly the set of elements that must be extracted and reinserted.
Example 2
current = [1,2,3]desired = [1,2,3]return = 0The unmatched suffix is exactly the set of elements that must be extracted and reinserted.
Example 3
current = [1,2,3]desired = [3,2,1]return = 2The unmatched suffix is exactly the set of elements that must be extracted and reinserted.
Constraints
1 <= current.length == desired.length <= 200000.- The arrays contain the same multiset.
Source note: Author-written prompt excerpt retained for source fidelity.