g2701_2800.s2766_relocate_marbles.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g2701_2800.s2766_relocate_marbles;
// #Medium #Array #Hash_Table #Sorting #Simulation
// #2023_09_24_Time_47_ms_(91.67%)_Space_60.2_MB_(36.33%)
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Solution {
public List relocateMarbles(int[] nums, int[] moveFrom, int[] moveTo) {
Set set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
for (int i = 0; i < moveTo.length; i++) {
if (set.contains(moveFrom[i])) {
set.remove(moveFrom[i]);
set.add(moveTo[i]);
}
}
List result = new ArrayList<>(set);
result.sort(null);
return result;
}
}