275. H-Index II Medium
Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper and citations is sorted in ascending order, return the researcher's h-index.
According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.
You must write an algorithm that runs in logarithmic time.
Example 1:
Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had received 0, 1, 3, 5, 6 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,2,100]
Output: 2
Approach
Input: An array citations representing the number of citations for each of the researcher's papers.
Output: Return the h-index.
This problem belongs to the Binary Search on Boundary category.
We need to find the maximum h such that the researcher has at least h papers with >= h citations.
We can use binary search to narrow down the range. Let's assume h = len(citations) - mid.
If
citations[mid] >= len(citations) - mid, it means all papers frommidto the end have citation counts greater than or equal tolen(citations) - mid. Therefore, the maximumhmight be on the left side ofmid(includingmid), so we continue searching to the left by settingright = mid - 1.Conversely, if the citation count at
midis less than the remaining number of papers, it doesn't satisfy the definition ofh. The maximumhmust be on the right side ofmid, so we continue searching to the right by settingleft = mid + 1.Finally, the position where
leftends up is the index corresponding to the maximumh. We can obtain the maximumhbylen(citations) - left, which is the total number of papers with citations>= h.Suppose there is only one paper, then
left == right == mid == 0. We still need to check ifcitations[mid] >= len(citations) - mid. Therefore, our terminating condition isleft > right.
Implementation
class Solution:
def hIndex(self, citations: List[int]) -> int:
n = len(citations) # Get the length of the citations array
left, right = 0, n - 1 # Initialize the left and right pointers for binary search
while left <= right: # Continue the loop when the left pointer is less than or equal to the right pointer
mid = (left + right) // 2 # Calculate the middle index
# If the number of citations at the middle position >= the remaining number of papers (n - mid)
# It means the h-index might be on the left side (including mid), move the right pointer to the left
if citations[mid] >= n - mid:
right = mid - 1
# Otherwise, the h-index is on the right side, move the left pointer to the right
else:
left = mid + 1
# Finally, left will stop at the boundary that satisfies the condition, n - left is the h-index
# Because n - left means there are at least n - left papers with citations >= n - left
return n - left/**
* @param {number[]} citations
* @return {number}
*/
var hIndex = function(citations) {
let left = 0;
let right = citations.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (citations[mid] >= citations.length - mid) {
right = mid - 1;
} else {
left = mid + 1
}
}
return citations.length - left;
};Complexity Analysis
- Time Complexity:
O(log n) - Space Complexity:
O(1)