码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 1051. 高度检查器

时间:2020-05-29 09:53:51      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:数组   com   for   ble   res   ret   htc   check   inline   

LeetCode 1051. 高度检查器

2. Tag

  1. 数组
  2. 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;
    }
}; 

LeetCode 1051. 高度检查器

标签:数组   com   for   ble   res   ret   htc   check   inline   

原文地址:https://www.cnblogs.com/HurryXin/p/12985362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!