The H-Index is a metric that aims to measure both the productivity and citation impact of the publications of a scientist or scholar. Given an array of integers citations
where citations[i]
is the number of citations a researcher received for their i-th paper, calculate the researcher’s H-Index.
The H-Index is defined as follows: A scientist has an index h
if h
of their n
papers have at least h
citations each, and the other n - h
papers have no more than h
citations each.
To determine the H-Index, let’s follow these steps:
citations
array in non-decreasing order.h
where citations[i] >= h
.Below is the implementation in C++:
#include <vector>
#include <algorithm>
int hIndex(std::vector<int>& citations) {
std::sort(citations.begin(), citations.end(), std::greater<int>());
int h = 0;
for (int i = 0; i < citations.size(); i++) {
if (citations[i] >= i + 1) {
h = i + 1;
} else {
break;
}
}
return h;
}
h
where there are at least h
papers with h
or more citations.h
to be the maximum value of i + 1
where the condition holds.O(n log n)
, where n
is the number of papers.O(n)
, iterating through the sorted array.Thus, the overall time complexity is O(n log n)
. This efficiency is suitable for typical input sizes in coding interviews and real-world scenarios.
I hope this helps! Feel free to ask if you have any questions or need further clarifications.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?