Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Sorting is a natural solution. But, you don't have to run O(nlgn) sorting for all the time. Counting sort is O(n)!

class Solution {
public:
    int hIndex(vector<int>& cit) 
    {
        int len = cit.size();
        vector<long> hist(cit.size()+1,0);
        for(int i=0;i<len;++i)
            hist[min<int>(len,cit[i])]++;
    
        long cumSum=0;
        for(int i=len;i>=0;--i) 
        {
            cumSum+=hist[i];
            if(cumSum>=i)
                return i;
        }        
        return 0;
    }
};
posted on 2015-09-04 04:58  Tonix  阅读(177)  评论(0编辑  收藏  举报