The H-Index is a metric that measures both the productivity and citation impact of the publications of a scholar. Given an array of integers citations
where citations[i]
is the number of citations a researcher received for their i-th
paper, write a function to compute the researcher’s H-Index.
The definition of the H-Index is as follows:
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.Example 1:
Input: citations = [3, 0, 6, 1, 5]
Output: 3
Explanation: [3, 0, 6, 1, 5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their H-index is 3.
Example 2:
Input: citations = [1, 3, 1]
Output: 1
To find the H-Index, follow these steps:
citations[i] ≥ i + 1
holds true.import java.util.Arrays;
public class Solution {
public int hIndex(int[] citations) {
// Step 1: Sort the array in descending order
Arrays.sort(citations);
int n = citations.length;
int hIndex = 0;
// Step 2: Iterate over the sorted array and determine the h-index
for (int i = 0; i < n; i++) {
int hCandidate = n - i;
if (citations[i] >= hCandidate) {
hIndex = hCandidate;
break; // No need to continue since we found the max h-index
}
}
return hIndex;
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] citations1 = {3, 0, 6, 1, 5};
System.out.println(solution.hIndex(citations1)); // Output: 3
int[] citations2 = {1, 3, 1};
System.out.println(solution.hIndex(citations2)); // Output: 1
}
}
citations
array.Since the sorting step dominates, the overall time complexity of the solution is (O(N \log N)).
This approach optimizes the calculation and ensures that we correctly find the H-Index efficiently.
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?