您的位置:首页 > 其它

H-Index II

2016-03-03 00:44 405 查看
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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: