Cover image for Interview Classic 150 Questions P274 H-Index

Interview Classic 150 Questions P274 H-Index


Timeline

timeline

2025-10-03

init

counting sort

Title:

After sorting, citations[i] indicates that there are n-i papers with citations >= citations[i]. We can directly traverse h to find the maximum value.

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
#include <algorithm>
#include <vector>

using std::vector;

class Solution {
public:
// A researcher's h-index means that he/she has published at least h papers
// and at least h papers have been cited at least h times. If there are multiple possible values of h, the h-index
// is the largest one.
int hIndex(vector<int> &citations)
{
int n = citations.size();
int h = 0;

std::sort(citations.begin(), citations.end());

for (int i = 1; i <= n; i++) { // traverse h
if (citations[n - i] >= i) {
h = i;
}
}
return h;
}
};