-
Notifications
You must be signed in to change notification settings - Fork 0
/
RelativeSortArray.cpp
39 lines (31 loc) · 988 Bytes
/
RelativeSortArray.cpp
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
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
unordered_map<int, int> freq;
for (int i = 0; i < arr2.size(); i++) {
freq[arr2[i]] = 0;
}
vector<int> tail;
for (int i = 0; i < arr1.size(); i++) {
unordered_map<int, int>::iterator found = freq.find(arr1[i]);
if (found == freq.end()) { // not found
tail.push_back(arr1[i]);
} else {
found->second++;
}
}
vector<int> output(arr1.size());
int idx = 0;
for (int i = 0; i < arr2.size(); i++) {
int cnt = freq[arr2[i]];
while (cnt > 0) { // do cnt time
output[idx] = arr2[i];
idx++;
cnt--;
}
}
sort(tail.begin(), tail.end());
move(tail.begin(), tail.end(), output.begin() + idx);
return output;
}
};