-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe1122.java
40 lines (33 loc) · 1.02 KB
/
e1122.java
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
35
36
37
38
39
40
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
HashSet<Integer> arr2vals = new HashSet<>();
for (Integer i : arr2) {
arr2vals.add(i);
}
HashMap<Integer, Integer> cnt = new HashMap<>();
for (Integer i : arr1) {
cnt.put(i, cnt.getOrDefault(i, 0) + 1);
}
int indx = 0;
int[] output = new int[arr1.length];
for (int i : arr2) {
for (int j = 0; j < cnt.get(i); j++, indx++) {
output[indx] = i;
}
cnt.remove(i);
}
int remainderIndx = 0;
int[] remainingKeys = new int[cnt.size()];
for (Integer i : cnt.keySet()) {
remainingKeys[remainderIndx] = i;
remainderIndx++;
}
Arrays.sort(remainingKeys);
for (Integer i : remainingKeys) {
for (int j = 0; j < cnt.get(i); j++, indx++) {
output[indx] = i;
}
}
return output;
}
}