275. H-Index II

来源:互联网 发布:仓库管理php源码 编辑:程序博客网 时间:2024/05/22 02:54

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

Hint:

  1. Expected runtime complexity is in O(log n) and the input is sorted.
题意:基于上题,假设已经排好序,则如何在O(logn)时间内找到H-index.

思路:二分查找。

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







0 0