-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1122.cpp
More file actions
24 lines (22 loc) · 692 Bytes
/
1122.cpp
File metadata and controls
24 lines (22 loc) · 692 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
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) { //O(N log N)
unordered_map<int,int> m;
for(int i = 0; i < arr2.size(); i++){
m[arr2[i]] = i;
}
vector<int> num(arr2.size());
vector<int> other;
for(int i : arr1){
if(m.count(i)) num[m[i]]++;
else other.push_back(i);
}
sort(other.begin(), other.end());
vector<int> sol;
for(int i = 0; i < arr2.size(); i++){
for(int j = 0; j < num[i]; j++) sol.push_back(arr2[i]);
}
for(int i : other) sol.push_back(i);
return sol;
}
};