|
| 1 | +package com.longluo.contest.biweekly_contest_108; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * https://leetcode.cn/contest/biweekly-contest-108 |
| 7 | + */ |
| 8 | +public class Problem2 { |
| 9 | + |
| 10 | + public static List<Integer> relocateMarbles(int[] nums, int[] moveFrom, int[] moveTo) { |
| 11 | + Map<Integer, Integer> posMap = new HashMap<>(); |
| 12 | + |
| 13 | + for (int x : nums) { |
| 14 | + posMap.put(x, posMap.getOrDefault(x, 0) + 1); |
| 15 | + } |
| 16 | + |
| 17 | + for (int i = 0; i < moveFrom.length; i++) { |
| 18 | + int from = moveFrom[i]; |
| 19 | + int to = moveTo[i]; |
| 20 | + |
| 21 | + if (from == to) { |
| 22 | + continue; |
| 23 | + } |
| 24 | + |
| 25 | + posMap.put(to, posMap.getOrDefault(to, 0) + posMap.get(from)); |
| 26 | + posMap.remove(from); |
| 27 | + } |
| 28 | + |
| 29 | + List<Integer> ans = new ArrayList<>(); |
| 30 | + |
| 31 | + for (int key : posMap.keySet()) { |
| 32 | + ans.add(key); |
| 33 | + } |
| 34 | + |
| 35 | + Collections.sort(ans); |
| 36 | + |
| 37 | + return ans; |
| 38 | + } |
| 39 | + |
| 40 | + public static void main(String[] args) { |
| 41 | + System.out.println("[1] ?= " + relocateMarbles(new int[]{3, 4}, new int[]{4, 3, 1, 2, 2, 3, 2, 4, 1}, new int[]{3, 1, 2, 2, 3, 2, 4, 1, 1})); |
| 42 | + System.out.println("[11, 20, 23] ?= " + relocateMarbles(new int[]{4, 6, 6, 9, 18}, new int[]{18, 6, 17, 4, 9, 19, 2}, new int[]{23, 17, 20, 19, 11, 2, 20})); |
| 43 | + System.out.println("[5, 6, 8, 9] ?= " + relocateMarbles(new int[]{1, 6, 7, 8}, new int[]{1, 7, 2}, new int[]{2, 9, 5})); |
| 44 | + System.out.println("[2] ?= " + relocateMarbles(new int[]{1, 1, 3, 3}, new int[]{1, 3}, new int[]{2, 2})); |
| 45 | + } |
| 46 | +} |
0 commit comments