274. H-Index
Efficient Solution for H-Index II Using Binary Search
In this post, we will discuss an efficient solution for the H-Index II problem using binary search.
Problem Statement
The H-Index is a metric used to measure the productivity and impact of a researcher's publications. Given an array of integers citations[] where each integer represents the number of citations a paper has received, the task is to find the researcher's H-Index.
A scientist has an H-Index if h of their papers have at least h citations each, and the remaining papers have no more than h citations.
In this variation of the problem (H-Index II), the citations[] array is sorted in ascending order.
Approach
Given that the array is already sorted, we can leverage binary search to find the H-Index efficiently. Binary search reduces the time complexity to O(log n), making it ideal for large datasets.
Key Observation
For a sorted array citations[], if we are at index mid, then the number of papers that have at least citations[mid] citations is size - mid (where size is the length of the array).
We are looking for the largest h such that:
citations[mid] >= size - mid- In other words, the number of papers that have at least
citations[mid]citations is greater than or equal tocitations[mid].
Solution Explanation
Here is the JavaScript code that implements the binary search solution:
javascriptvar hIndex = function(citations) {
// Get the size of the array
var size = citations.length;
// Initialize the low and high pointers for binary search
let low = 0;
let high = size - 1;
// Variable to store the maximum h-index found
var h = 0;
// Binary search loop
while (low <= high) {
// Find the mid-point of the current range
var mid = Math.floor((low + high) / 2);
// Check if the number of papers with at least citations[mid] is enough
if (citations[mid] >= size - mid) {
// If yes, update h to the current number of papers
h = size - mid;
// Move to the left part to find a higher h-index
high = mid - 1;
} else {
// Otherwise, move to the right part
low = mid + 1;
}
}
// Return the found h-index
return h;
};
java
class Solution { public int hIndex(int[] citations) { // binary search apply int size = citations.length; int low = 0; int high = size - 1; int h = 0; while (low <= high) { int mid = (low + high) / 2; if(citations[mid] >= size - mid) { h = size - mid; high = mid - 1; }else { low = mid + 1; } } return h;}}Step-by-Step Explanation
Initialization:
- We initialize the
lowpointer at 0 (beginning of the array) and thehighpointer atsize - 1(end of the array). his used to track the maximum H-Index we find during the binary search.
- We initialize the
Binary Search Loop:
- At each step, we calculate the
midindex as the average oflowandhigh. - We check if
citations[mid] >= size - mid. This checks if the number of papers with at leastcitations[mid]citations is greater than or equal tocitations[mid]itself. If this condition holds, it means the current mid-point can be a candidate for the H-Index. - If the condition is true, we update
htosize - midand move thehighpointer tomid - 1to check for higher possible H-Index values. - If the condition is false, it means we need to look to the right of the current mid-point, so we move the
lowpointer tomid + 1.
- At each step, we calculate the
Return the H-Index:
- The loop runs until
lowexceedshigh, and we return the maximum H-Index found during the binary search.
- The loop runs until
Time Complexity
The time complexity of this algorithm is O(log n), thanks to the binary search approach. This is much faster than the brute force method, which would require linear time O(n) to check each element.
Space Complexity
The space complexity is O(1), as we are only using a constant amount of extra space.
Conclusion
This binary search-based solution is a highly efficient way to solve the H-Index II problem, especially for large arrays of citation counts. By exploiting the sorted nature of the input array, we reduce the problem to a logarithmic time complexity solution.
Feel free to use this approach to solve the H-Index II problem on LeetCode or any similar coding platform!
Comments
Post a Comment