-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHIndexII.cpp
More file actions
61 lines (54 loc) · 1.08 KB
/
HIndexII.cpp
File metadata and controls
61 lines (54 loc) · 1.08 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
void print(vector<int> &v){
for(auto i:v){
cout << i << " ";
}
cout << endl;
}
class Solution {
public:
// int hIndex(vector<int>& citations) {
// int h = 0;
// int size = citations.size();
// for(int i=size-1; i>=0; i--){
// int n = citations[i];
// int tmp_h = min(n, size-i);
// if(h<tmp_h)
// h = tmp_h;
// }
// return h;
// }
int hIndex(vector<int>& citations) {
int lo = 0;
int hi = citations.size()-1;
int max_h = 0;
int size = citations.size();
while(lo <= hi){
int mid = lo + (hi-lo)/2;
int n = citations[mid];
int h;
if(n > size-mid){
h = size-mid;
hi = mid-1;
} else{
h = n;
lo = mid+1;
}
if(h > max_h)
max_h = h;
}
return max_h;
}
};
int main(int argc, char *argv[]) {
Solution sol;
vector<int> citations{0,1,3,5,6};
cout << sol.hIndex(citations) << endl;
return 0;
}