274. H-Index ๐
Difficulty: Medium
- Tags: Array
, Sorting
Description
Given an array of integers citations
where citations[i]
is the number of citations a researcher received for their ith
paper, return the researcher's h-index
.
The h-index is defined as the maximum value of h
such that the researcher has published at least h
papers that have each been cited at least h
times.
Examples
Example 1:
Input:
Output:
Explanation: The researcher has 5 papers in total, with citation counts of [3,0,6,1,5]
.
The h-index is 3 because there are 3 papers with at least 3 citations each, and the remaining 2 papers have fewer than 3 citations.
Example 2:
Input:
Output:
Explanation: The researcher has 3 papers with citation counts [1,3,1]
.
The h-index is 1 because there is 1 paper with at least 1 citation, and the remaining papers have fewer than 1 citation.
Solution ๐ก
Sort the citations array in descending order. The h-index is the maximum value of h
such that there are at least h
papers with h
or more citations.
Java
Time Complexity โณ
Sorting the array takes
O(n log n)
, wheren
is the number of citations.The for loop iterates through the sorted array, taking
O(n)
time.
Thus, the overall time complexity is O(n log n).
Space Complexity ๐พ
The space complexity is O(1) since the sorting is done in-place and we are using only a few extra variables.
You can find the full Solution.java
file here and the best Solution is here.
Last updated