1331.cpp (502B)
1 class Solution { 2 public: 3 vector<int> arrayRankTransform(const vector<int> &arr) const { 4 const int n = size(arr); 5 vector<pair<int, int>> vec(n); 6 vector<int> ans(n); 7 8 for (int i = 0; int x : arr) 9 vec[i++] = {x, i}; 10 sort(begin(vec), end(vec)); 11 12 int curr = 0, prev = INT_MIN; 13 for (const auto &[x, i] : vec) { 14 if (x > prev) curr++; 15 ans[i] = curr; 16 prev = x; 17 } 18 19 return ans; 20 } 21 };