H-Index II

来源:互联网 发布:记账app 知乎 编辑:程序博客网 时间:2024/05/19 09:16

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

class Solution {public:    int hIndex(vector<int>& citations) {        int n = citations.size();        if (n < 1)        {            return 0;        }        int left = 0;        int right = n-1;        while (left < right-1)        {            int mid = left + (right-left)/2;            if (citations[mid] >= n-mid)            {                right = mid;            }            else            {                left = mid;            }        }        if (citations[left] >= n-left)        {            return n-left;        }        else if (citations[right] >= n-right)        {            return n-right;        }        return 0;    }};


0 0