-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path1122.Relative-Sort-Array.java
More file actions
39 lines (35 loc) · 958 Bytes
/
1122.Relative-Sort-Array.java
File metadata and controls
39 lines (35 loc) · 958 Bytes
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
// https://leetcode.com/problems/relative-sort-array/
//
// algorithms
// Easy (68.39%)
// Total Accepted: 3,572
// Total Submissions: 5,223
// beats 100.0% of java submissions
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int n : arr2) {
map.put(n, 0);
}
ArrayList<Integer> l = new ArrayList<>();
for (int n : arr1) {
if (map.containsKey(n)) {
map.put(n, map.get(n) + 1);
} else {
l.add(n);
}
}
Collections.sort(l);
int[] res = new int[arr1.length];
int idx = 0;
for (int n : arr2) {
for (int j = 0; j < map.get(n); j++) {
res[idx++] = n;
}
}
for (int n : l) {
res[idx++] = n;
}
return res;
}
}