H-Index II - Problem

The H-Index is a fascinating metric used to measure a researcher's academic impact! ๐ŸŽ“

Given a sorted array of integers citations where citations[i] represents the number of citations a researcher received for their i-th paper, your task is to find their H-Index.

What is 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.

The Challenge: Since the array is already sorted, you must write an algorithm that runs in O(log n) time - think binary search! ๐Ÿ”

Example: If a researcher has papers with citations [0, 1, 3, 5, 6], they have 3 papers with at least 3 citations each (papers with 3, 5, and 6 citations), so their H-Index is 3.

Input & Output

example_1.py โ€” Basic Case
$ Input: citations = [0,1,3,5,6]
โ€บ Output: 3
๐Ÿ’ก Note: The researcher has 3 papers with at least 3 citations each (papers with 3, 5, and 6 citations), and the remaining 2 papers have โ‰ค 3 citations. So H-Index = 3.
example_2.py โ€” All High Citations
$ Input: citations = [1,2,100]
โ€บ Output: 2
๐Ÿ’ก Note: The researcher has 2 papers with at least 2 citations each (papers with 2 and 100 citations). Can't have H-Index = 3 because there are only 3 papers total, but only 1 paper has โ‰ฅ 3 citations.
example_3.py โ€” All Zero Citations
$ Input: citations = [0,0,0,0]
โ€บ Output: 0
๐Ÿ’ก Note: No papers have any citations, so the H-Index is 0. The researcher has 0 papers with at least 1 citation each.

Constraints

  • 1 โ‰ค citations.length โ‰ค 105
  • 0 โ‰ค citations[i] โ‰ค 1000
  • citations is sorted in non-decreasing order
  • Follow up: This is a follow-up problem to H-Index, where citations is not sorted.

Visualization

Tap to expand
H-Index Binary Search VisualizationSorted Citations Arrayi=00i=11i=23i=35i=46Binary Search ProcessCheck Position i=2 (middle)citations[2] = 3, papers_count = 5-2 = 3Since 3 โ‰ฅ 3, H-Index โ‰ฅ 3 is valid!Search left half for potentially larger H-IndexH-Index3Maximum valid H-Index found!๐ŸŽฏ Key Insight:Binary search leverages the sorted property to find the transition point wherewe have exactly h papers with โ‰ฅ h citations in O(log n) time!
Understanding the Visualization
1
Understanding the Problem
We need to find the maximum h where we have โ‰ฅh papers with โ‰ฅh citations each
2
Key Insight
In a sorted array, position i has exactly (n-i) papers from i to the end
3
Binary Search Logic
If citations[i] โ‰ฅ (n-i), then H-Index could be (n-i) or larger
4
Final Result
Binary search finds the optimal position efficiently
Key Takeaway
๐ŸŽฏ Key Insight: Binary search finds the optimal H-Index by leveraging the sorted array property to locate the transition point efficiently in O(log n) time.
Asked in
Google 45 Facebook 32 Amazon 28 Microsoft 24
42.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen