1122.cpp (516B)
1 class Solution { 2 public: 3 vector<int> relativeSortArray(vector<int> &arr1, const vector<int> &arr2) const { 4 const int n = size(arr2); 5 unordered_map<int, int> um; 6 7 for (int i = 0; i < n; i++) 8 um[arr2[i]] = i; 9 sort(begin(arr1), end(arr1), [&n, &um](int a, int b) { 10 const auto [it1, _1] = um.emplace(a, n + a); 11 const auto [it2, _2] = um.emplace(b, n + b); 12 13 return it1->second < it2->second; 14 }); 15 16 return arr1; 17 } 18 };