-
Notifications
You must be signed in to change notification settings - Fork 28
/
H-Index_II.cpp
38 lines (38 loc) · 896 Bytes
/
H-Index_II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// O(logn)
class Solution {
public:
int hIndex(vector<int>& citations) {
if(citations.empty()) return 0;
int n = citations.size();
int hIndx = 0;
int left = 0, right = n - 1;
while(left <= right) {
if(left == right) {
return min(citations[left], n - left);
}
int mid = left + (right - left) / 2;
if(citations[mid] < n - mid) {
left = mid + 1;
} else {
hIndx = n - mid;
right = mid;
}
}
return hIndx;
}
};
// O(n)
/*
class Solution {
public:
int hIndex(vector<int>& citations) {
int hIndx = 0;
int n = citations.size();
for(int i = n - 1; i >= 0; --i) {
if(citations[i] < n - i) break;
hIndx = n - i;
}
return hIndx;
}
};
*/