h-index 1.0.0
H-Index
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <vector>
3#include <map>
4
5using namespace std;
6
8{
9public:
10 int hIndex(vector<int>& citations)
11 {
12 int length = (int)citations.size();
13 if (length == 0)
14 return 0;
15
16 // h-index (f) = max { i e N : f(i) >= i }
17 // we cannot use binary search due to list being not sorted. sorting and using binary search would consume more complexity than O(n)
18 // let's use a vector to count citations i in count[i], so that when 2,2 will be saved to citation[2] = 2
19
20 vector<int> count(length + 1, 0);
21 for (int citation : citations)
22 {
23 if (citation >= length)
24 count[length]++; // citation counts larger than length are stored in a last field of array
25 else
26 count[citation]++;
27 }
28
29 // for (int i = 0; i < length + 1; ++i)
30 // cout << "count[" << i << "]: " << count[i] << endl;
31
32 int total = 0;
33 // starting from last field ensures only-entries-bigger-than-length are handled properly in the first place
34 for (int i = length; i >= 0; --i)
35 {
36 total += count[i];
37 if (total >= i)
38 return i;
39 }
40
41 return 0;
42 }
43};
44
45int main()
46{
47 // vector<int> array = {3, 0, 6, 1, 5};
48 // vector<int> array = {11, 15};
49 vector<int> array = {1, 2, 0};
50 Solution sol;
51 cout << "h-index: " << sol.hIndex(array) << endl;
52 return 0;
53}
int hIndex(vector< int > &citations)
Definition main.cpp:10
int main()
Definition main.cpp:45