- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1122.java
29 lines (27 loc) · 908 Bytes
/
_1122.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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.Arrays;
importjava.util.TreeMap;
publicclass_1122 {
publicstaticclassSolution1 {
publicint[] relativeSortArray(int[] arr1, int[] arr2) {
TreeMap<Integer, Integer> map = newTreeMap<>();
Arrays.stream(arr1).forEach(num -> map.put(num, map.getOrDefault(num, 0) + 1));
int[] result = newint[arr1.length];
inti = 0;
for (intnum : arr2) {
intcount = map.get(num);
while (count-- > 0) {
result[i++] = num;
}
map.remove(num);
}
for (intkey : map.keySet()) {
intcount = map.get(key);
while (count-- > 0) {
result[i++] = key;
}
}
returnresult;
}
}
}