-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2766-relocate-marbles.js
More file actions
34 lines (31 loc) · 1.04 KB
/
2766-relocate-marbles.js
File metadata and controls
34 lines (31 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* 2766. Relocate Marbles
* https://leetcode.com/problems/relocate-marbles/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number[]} nums
* @param {number[]} moveFrom
* @param {number[]} moveTo
* @return {number[]}
*/
var relocateMarbles = function(nums, moveFrom, moveTo) {
const set = new Set(nums);
for (let i = 0; i < moveFrom.length; i++) {
set.delete(moveFrom[i]);
set.add(moveTo[i]);
}
return [...set].sort((a, b) => a - b);
};