#275

H-Index II

Medium
ArrayBinary SearchBinary SearchArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n)
O(log n)
Space
O(1)
O(1)
💡

Intuition

Time O(log n)Space O(1)

Using binary search takes advantage of the sorted nature of the citations array. We can efficiently find the maximum h-index by narrowing down the search space.

⚙️

Algorithm

5 steps
  1. 1Step 1: Initialize left pointer to 0 and right pointer to the length of citations minus one.
  2. 2Step 2: While left pointer is less than or equal to right pointer, calculate the mid-point.
  3. 3Step 3: Check if the number of papers (n - mid) is at least mid + 1.
  4. 4Step 4: If true, move the left pointer to mid + 1. Else, move the right pointer to mid - 1.
  5. 5Step 5: The left pointer will point to the maximum h-index after the loop.
solution.py11 lines
1# Full working Python code
2
3def hIndex(citations):
4    left, right = 0, len(citations) - 1
5    while left <= right:
6        mid = (left + right) // 2
7        if citations[mid] >= mid + 1:
8            left = mid + 1
9        else:
10            right = mid - 1
11    return right + 1

Complexity note: This complexity is achieved because we are using binary search, which divides the search space in half with each iteration.

  • 1The h-index is determined by the number of papers that have at least h citations.
  • 2The sorted nature of the citations array allows for efficient searching using binary search.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.