1. LeetCode Link
2. Tag
- 数组
- Optimize
3. Code
桶排序,时间复杂度\(O(n)\)
class Solution {
public:
int heightChecker(vector<int>& heights) {
int cout[105]={0};
int res=0;
for(auto height:heights){
cout[height]++;
}
int j=0;
for(int i=1;i<105;i++){
while(cout[i]--){
if(heights[j++]!=i) res++;
}
}
return res;
}
};