Re: [闲聊] 每日leetcode

楼主: sixB (6B)   2024-10-02 21:27:26
1331.
今天好简单
sort by index
一个一个加上去
怎么大家都用map解==我吐了
class Solution {
public:
vector<int> arrayRankTransform(vector<int>& arr) {
int n = arr.size();
if(n == 0) return {};
vector<int> indice(n, 0);
for(int i = 0; i < n; i++){
indice[i] = i;
}
ranges::sort(indice, [&](int a, int b){return arr[a] < arr[b]; });
for(int i = 0, cnt = 1, cur = arr[indice[0]]; i < n; i++){
int idx = indice[i];
if(arr[idx] > cur) cnt++;
cur = arr[idx];
arr[idx] = cnt;
}
return arr;
}
};

Links booklink

Contact Us: admin [ a t ] ucptt.com