-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1122.cpp
More file actions
34 lines (34 loc) · 907 Bytes
/
1122.cpp
File metadata and controls
34 lines (34 loc) · 907 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
class Compare {
private:
unordered_map<int, int> mp;
public:
Compare(unordered_map<int, int>& mp) {
this->mp = mp;
}
bool operator()(const int& x, const int& y) {
auto itX = mp.find(x);
auto itY = mp.find(y);
if (itX != mp.end() && itY != mp.end()) {
return mp[x] < mp[y];
}
else if (itX != mp.end() && itY == mp.end()) {
return true;
}
else if (itX == mp.end() && itY != mp.end()) {
return false;
}
else return x < y;
}
};
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
unordered_map<int, int> mp;
for (int i = 0; i < arr2.size(); ++i) {
mp[arr2[i]] = i;
}
Compare* compare = new Compare(mp);
sort(arr1.begin(), arr1.end(), (*compare));
return arr1;
}
};