H-Index II - Problem

Given an array of integers citations where citations[i] is the number of citations a researcher received for their i-th paper and citations is sorted in non-descending 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.

Input & Output

Example 1 — Standard Case
$ Input: citations = [0,1,3,5,6]
Output: 3
💡 Note: The researcher has 5 papers with [0,1,3,5,6] citations. 3 papers have at least 3 citations each (papers with 3,5,6 citations), and the other 2 papers have ≤ 3 citations. So h-index is 3.
Example 2 — Higher Citations
$ Input: citations = [1,2,100]
Output: 2
💡 Note: The researcher has 3 papers with [1,2,100] citations. 2 papers have at least 2 citations each (papers with 2,100 citations), and 1 paper has ≤ 2 citations. So h-index is 2.
Example 3 — All Zero Citations
$ Input: citations = [0,0]
Output: 0
💡 Note: Both papers have 0 citations, so no papers have at least 1 citation. H-index is 0.

Constraints

  • 1 ≤ citations.length ≤ 105
  • 0 ≤ citations[i] ≤ 1000
  • citations is sorted in non-descending order

Visualization

Tap to expand
H-Index II - Optimized Linear Scan INPUT Sorted citations array: 0 i=0 1 i=1 3 i=2 5 i=3 6 i=4 Papers from index i to end: 5 4 3 2 1 (n - i papers remaining) Given: citations = [0,1,3,5,6] n = 5 papers (sorted) ALGORITHM STEPS 1 Start from left Scan array from index 0 2 Calculate papers left h = n - i (papers remaining) 3 Check condition citations[i] >= h ? 4 Return h-index First valid h found Linear Scan Process: i c[i] h=n-i c[i]>=h? 0 0 5 NO 1 1 4 NO 2 3 3 OK 3 5 2 -- FINAL RESULT H-Index Found at i=2: h-index = 3 Interpretation: At i=2, citations[2] = 3 Papers remaining = 5 - 2 = 3 3 papers with at least 3 cites The 3 papers (3, 5, 6 cites): 3 5 6 All >= 3 citations each Output: 3 Key Insight: Since array is sorted, papers from index i to end all have citations >= citations[i]. We scan left-to-right: when citations[i] >= (n-i), we found h = n-i papers with at least h citations. Time Complexity: O(n) for linear scan | Can be optimized to O(log n) with binary search TutorialsPoint - H-Index II | Optimized Linear Scan Approach Time: O(n) | Space: O(1)
Asked in
Google 25 Facebook 20 Amazon 15
28.0K Views
Medium Frequency
~15 min Avg. Time
890 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