LandscapeMi

landscapemi的博客

leetcode :Binary Search: H-Index IIe(275)

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

Expected runtime complexity is in O(log n) and the input is sorted.


class Solution {
public:
    // binary search - O(log(n))
    int hIndex(vector<int>& citations) {
        int n = citations.size();
        int low = 0, high = n-1;

        while( low <= high ) {
            int mid = low + (high-low)/2;
            if (citations[mid] == n - mid) {
                return n - mid;
            }else if (citations[mid] > n-mid){
                high = mid - 1;
            }else {
                low = mid + 1;
            }
        }
        return n-low;
    }
};
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mijian1207mijian/article/details/52346348
个人分类: leetcode(c++)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

leetcode :Binary Search: H-Index IIe(275)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭