
Sponsored
Sponsored
This approach uses sorting to calculate the h-index. The idea is to sort the array of citations in descending order. Then, find the maximum number h such that there are h papers with at least h citations. This can be efficiently determined by iterating over the sorted array.
Time Complexity: O(n log n) due to sorting, Space Complexity: O(1) since the sorting is in place.
1function hIndex(citations) {
2 citations.sort((a, b) => b - a);
3 for (let i = 0; i < citations.length; i++) {
4 if (citations[i] < i + 1) {
5 return i;
6 }
7 }
8 return citations.length;
9}
10
11const citations = [3, 0, 6, 1, 5];
12console.log("H-Index:", hIndex(citations));The JavaScript version sorts the citations in descending order using the array sort method, and iterates over the sorted array to compute the h-index.
Given the constraints where citation counts do not exceed 1000 and the number of papers is at most 5000, a counting sort or bucket sort can be used. This approach involves creating a frequency array to count citations. Then traverse the frequency array to compute the h-index efficiently.
Time Complexity: O(n + m) where n is citationsSize and m is the maximum citation value, Space Complexity: O(m).
1public
The Java method utilizes a fixed-sized frequency array to accumulate citation counts and determine the h-index from reverse.