-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
53 lines (45 loc) · 1.46 KB
/
Copy pathmain.cpp
File metadata and controls
53 lines (45 loc) · 1.46 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
#include <iostream>
#include <vector>
#include <map>
using namespace std;
class Solution
{
public:
int hIndex(vector<int>& citations)
{
int length = (int)citations.size();
if (length == 0)
return 0;
// h-index (f) = max { i e N : f(i) >= i }
// we cannot use binary search due to list being not sorted. sorting and using binary search would consume more complexity than O(n)
// let's use a vector to count citations i in count[i], so that when 2,2 will be saved to citation[2] = 2
vector<int> count(length + 1, 0);
for (int citation : citations)
{
if (citation >= length)
count[length]++; // citation counts larger than length are stored in a last field of array
else
count[citation]++;
}
// for (int i = 0; i < length + 1; ++i)
// cout << "count[" << i << "]: " << count[i] << endl;
int total = 0;
// starting from last field ensures only-entries-bigger-than-length are handled properly in the first place
for (int i = length; i >= 0; --i)
{
total += count[i];
if (total >= i)
return i;
}
return 0;
}
};
int main()
{
// vector<int> array = {3, 0, 6, 1, 5};
// vector<int> array = {11, 15};
vector<int> array = {1, 2, 0};
Solution sol;
cout << "h-index: " << sol.hIndex(array) << endl;
return 0;
}