Skip to content

Practice Relocate Marbles

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2766
LeetCode Problem

Relocate Marbles

Problem Statement

nums gives initial marble positions, possibly with several marbles at one position. For each step i, move every marble at moveFrom[i] to moveTo[i]. Return the distinct occupied positions in increasing order after all steps. A position is occupied when it contains at least one marble.

Source: Relocate Marbles on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,6,7,8], moveFrom = [1,7,2], moveTo = [2,9,5]
Output
[5,6,8,9]

# Example 2

Input
nums = [1,1,3,3], moveFrom = [1,3], moveTo = [2,2]
Output
[2]

Constraints

1 <= nums.length <= 10^5
1 <= moveFrom.length <= 10^5
moveFrom.length == moveTo.length
1 <= nums[i], moveFrom[i], moveTo[i] <= 10^9
The test cases are generated such that there is at least a marble in moveFrom[i] at the moment we want to apply the ith move.

Related LeetCode Problems

Practice more problems similar to Relocate Marbles.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.