首页 > 其他 > 详细

LeetCode "H-Index"

时间:2015-09-04 07:33:00      阅读:1251      评论:0      收藏:0      [点我收藏+]

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;
    }
};

LeetCode "H-Index"

原文:http://www.cnblogs.com/tonix/p/4781184.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!