h-index 1.0.0
H-Index
Loading...
Searching...
No Matches
Solution Class Reference

Public Member Functions

int hIndex (vector< int > &citations)
 

Detailed Description

Definition at line 7 of file main.cpp.

Member Function Documentation

◆ hIndex()

int Solution::hIndex ( vector< int > &  citations)
inline

Definition at line 10 of file main.cpp.

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 }

Referenced by main().


The documentation for this class was generated from the following file: