-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1122.relative-sort-array.java
73 lines (67 loc) · 1.77 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
/*
* @lc app=leetcode id=1122 lang=java
*
* [1122] Relative Sort Array
*/
// @lc code=start
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] count = new int[1001];
for (int i = 0; i < arr1.length; ++i) {
++count[arr1[i]];
}
int cur = 0;
for (var n : arr2) {
while (count[n] != 0) {
arr1[cur++] = n;
--count[n];
}
}
for (var i=0;i<count.length;++i) {
while (count[i] != 0) {
arr1[cur++] = i;
--count[i];
}
}
return arr1;
}
}
// @lc code=end
// class Solution {
// public int[] relativeSortArray(int[] arr1, int[] arr2) {
// Map<Integer, Integer> comp_order = new HashMap<>();
// for (int i = 0; i != arr2.length; ++i) {
// comp_order.put(arr2[i], i);
// }
// List<Integer> list =
// Arrays.stream(arr1).boxed().collect(Collectors.toList());;
// Collections.sort(list, new MyComp(comp_order));
// return list.stream().mapToInt(i->i).toArray();
// }
// }
// class MyComp implements Comparator<Integer> {
// Map<Integer, Integer> comp_order;
// MyComp(Map<Integer, Integer> _comp_order) {
// comp_order = _comp_order;
// }
// @Override
// public int compare(Integer l, Integer r) {
// if (comp_order.containsKey(l) && comp_order.containsKey(r)) {
// return comp_order.get(l) - comp_order.get(r);
// } else if (comp_order.containsKey(l)) {
// return -1;
// } else if (comp_order.containsKey(r)) {
// return 1;
// } else {
// return l - r;
// }
// }
// }