LeetCode 275. H-Index II

1
2
3
4
5
6
7
8
class Solution {
public:
int hIndex(vector<int>& citations) {
int i = citations.size() - 1, h = 0;
while (i >= 0 && citations[i] > h) ++h, --i;
return h;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size(), l = 0, r = n;
auto check = [&](int h) {
int idx = lower_bound(citations.begin(), citations.end(), h) - citations.begin();
return n - idx >= h;
};
while (l < r) {
int mid = l + r + 1 >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
return l;
}
};