-
Notifications
You must be signed in to change notification settings - Fork 5
/
1122. Relative Sort Array.java
37 lines (37 loc) · 1.19 KB
/
1122. Relative Sort Array.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
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
for (int num : arr1) {
map.putIfAbsent(num, 0);
map.put(num, map.get(num) + 1);
}
int index = 0;
for (int key : arr2) {
// if key in map then populate in arr1
if (map.containsKey(key)) {
// insert the freq x times in arr1
int times = map.get(key);
while (times > 0) {
arr1[index] = key;
index++;
times--;
}
map.remove(key);
}
}
for (Map.Entry<Integer, Integer> kv : map.entrySet()) {
int key = kv.getKey();
int times = kv.getValue();
while (times > 0) {
arr1[index] = key;
index++;
times--;
}
}
return arr1;
}
}