2766.cpp (633B)
1 class Solution { 2 public: 3 vector<int> relocateMarbles(const vector<int> &nums, const vector<int> &moveFrom, 4 const vector<int> &moveTo) const { 5 unordered_map<int, int> um; 6 vector<int> res; 7 8 for (const int num : nums) 9 um[num]++; 10 for (int i = 0; i < size(moveFrom); i++) { 11 if (moveTo[i] == moveFrom[i]) continue; 12 um[moveTo[i]] += um[moveFrom[i]]; 13 um[moveFrom[i]] = 0; 14 } 15 16 for (const auto [k, v] : um) 17 if (v) res.push_back(k); 18 sort(begin(res), end(res)); 19 20 return res; 21 } 22 };