Relocate Marbles - Problem
You are given a 0-indexed integer array nums representing the initial positions of some marbles. You are also given two 0-indexed integer arrays moveFrom and moveTo of equal length.
Throughout moveFrom.length steps, you will change the positions of the marbles. On the ith step, you will move all marbles at position moveFrom[i] to position moveTo[i].
After completing all the steps, return the sorted list of occupied positions.
Notes:
- We call a position occupied if there is at least one marble in that position.
- There may be multiple marbles in a single position.
Input & Output
Example 1 — Basic Marble Movement
$
Input:
nums = [1,2,3], moveFrom = [1,3], moveTo = [2,5]
›
Output:
[2,5]
💡 Note:
Initially marbles at positions [1,2,3]. Move 1: all marbles at position 1 move to position 2, now at [2,2,3]. Move 2: all marbles at position 3 move to position 5, now at [2,2,5]. Occupied positions are [2,5].
Example 2 — Multiple Marbles at Same Position
$
Input:
nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2]
›
Output:
[2]
💡 Note:
Initially marbles at [1,1,3,3]. Move 1: marbles at position 1 move to 2, now [2,2,3,3]. Move 2: marbles at position 3 move to 2, now [2,2,2,2]. Only position 2 is occupied.
Example 3 — No Movement for Missing Position
$
Input:
nums = [1,2], moveFrom = [3], moveTo = [4]
›
Output:
[1,2]
💡 Note:
Initially marbles at [1,2]. Move 1: try to move from position 3 to 4, but no marbles at position 3. Final positions remain [1,2].
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ moveFrom.length == moveTo.length ≤ 105
- -109 ≤ nums[i], moveFrom[i], moveTo[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Initial marble positions and move operations
2
Process
Apply moves sequentially to relocate marbles
3
Output
Sorted list of final occupied positions
Key Takeaway
🎯 Key Insight: Track positions, not individual marbles - use hash set for efficient add/remove operations
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code