Intuition
Sort citations from most-cited to least. After sorting, the paper at 0-based index i is the (i+1)th most cited. The h-index is the largest i+1 for which that paper still has at least i+1 citations.
Explanation
Idea
Sort the citation counts in descending order. Now scanning left to right, at position i (0-based) we have i+1 papers each cited at least citations[i] times. The condition for an h-index of i+1 is exactly citations[i] >= i + 1. As we move right the paper count grows but the citation floor drops, so the h-index is the last position where the condition still holds.
Algorithm
- Sort
citationsdescending. - Walk
ifrom 0: whilecitations[i] >= i + 1, seth = i + 1. Stop at the first failure. - Return
h.
Worked example: [3,0,6,1,5]
Sorted desc: [6,5,3,1,0].
| i | citations[i] | i+1 | ≥? | h |
|---|---|---|---|---|
| 0 | 6 | 1 | yes | 1 |
| 1 | 5 | 2 | yes | 2 |
| 2 | 3 | 3 | yes | 3 |
| 3 | 1 | 4 | no | stop |
h-index = 3 (3 papers with ≥ 3 citations).
Complexity
- Time: O(n log n) for the sort.
- Space: O(1) beyond the sort.
Implementation
import java.util.Arrays;
class Solution {
public int hIndex(int[] citations) {
Arrays.sort(citations);
int n = citations.length;
int h = 0;
for (int i = 0; i < n; i++) {
int count = n - i;
if (citations[i] >= count) {
h = count;
break;
}
}
return h;
}
}