| 
 | 1 | +## 274. H-Index [Medium]  | 
 | 2 | + | 
 | 3 | +https://leetcode.com/problems/h-index  | 
 | 4 | + | 
 | 5 | +## Description  | 
 | 6 | +Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `iᵗʰ` paper, return *the researcher's h-index*.  | 
 | 7 | + | 
 | 8 | +According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): 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.  | 
 | 9 | + | 
 | 10 | +**Examples**  | 
 | 11 | + | 
 | 12 | +```tex  | 
 | 13 | +Example 1:  | 
 | 14 | +Input: citations = [3,0,6,1,5]  | 
 | 15 | +Output: 3  | 
 | 16 | +Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had received 3, 0, 6, 1, 5 citations respectively.  | 
 | 17 | +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.  | 
 | 18 | +
  | 
 | 19 | +Example 2:  | 
 | 20 | +Input: citations = [1,3,1]  | 
 | 21 | +Output: 1  | 
 | 22 | +```  | 
 | 23 | + | 
 | 24 | +**Constraints**  | 
 | 25 | +```tex  | 
 | 26 | +- n == citations.length  | 
 | 27 | +- 1 <= n <= 5000  | 
 | 28 | +- 0 <= citations[i] <= 1000  | 
 | 29 | +```  | 
 | 30 | + | 
 | 31 | +## Explanation  | 
 | 32 | + | 
 | 33 | +### Strategy  | 
 | 34 | +Let's restate the problem: You're given an array representing the number of citations for each paper a researcher has published. You need to find the maximum value `h` such that the researcher has at least `h` papers with at least `h` citations each.  | 
 | 35 | + | 
 | 36 | +This is a **sorting problem** that requires understanding the definition of h-index and finding the optimal value efficiently.  | 
 | 37 | + | 
 | 38 | +**What is given?** An array of integers representing citation counts for each paper.  | 
 | 39 | + | 
 | 40 | +**What is being asked?** Find the maximum h-index value that satisfies the h-index definition.  | 
 | 41 | + | 
 | 42 | +**Constraints:** The array can be up to 5000 elements long, with citation counts ranging from 0 to 1000.  | 
 | 43 | + | 
 | 44 | +**Edge cases:**   | 
 | 45 | +- Array with all zeros  | 
 | 46 | +- Array with all high citation counts  | 
 | 47 | +- Array with single element  | 
 | 48 | +- Array with mixed citation counts  | 
 | 49 | + | 
 | 50 | +**High-level approach:**  | 
 | 51 | +The solution involves understanding the h-index definition and using sorting to efficiently find the maximum valid h value.  | 
 | 52 | + | 
 | 53 | +**Decomposition:**  | 
 | 54 | +1. **Sort the array**: Arrange citations in descending order to easily check h-index conditions  | 
 | 55 | +2. **Iterate through sorted array**: Check each position as a potential h-index  | 
 | 56 | +3. **Verify h-index condition**: Ensure at least h papers have at least h citations  | 
 | 57 | +4. **Return maximum valid h**: Find the largest h that satisfies the condition  | 
 | 58 | + | 
 | 59 | +**Brute force vs. optimized strategy:**  | 
 | 60 | +- **Brute force**: Try each possible h value and check if it satisfies the condition. This takes O(n²) time.  | 
 | 61 | +- **Optimized**: Sort the array and use a single pass to find the h-index. This takes O(n log n) time.  | 
 | 62 | + | 
 | 63 | +### Steps  | 
 | 64 | +Let's walk through the solution step by step using the first example: `citations = [3,0,6,1,5]`  | 
 | 65 | + | 
 | 66 | +**Step 1: Sort the array in descending order**  | 
 | 67 | +- Original: `[3,0,6,1,5]`  | 
 | 68 | +- Sorted: `[6,5,3,1,0]`  | 
 | 69 | + | 
 | 70 | +**Step 2: Check each position as potential h-index**  | 
 | 71 | +- Position 0: `h = 1`, check if `citations[0] >= 1` ✓ (6 >= 1)  | 
 | 72 | +- Position 1: `h = 2`, check if `citations[1] >= 2` ✓ (5 >= 2)  | 
 | 73 | +- Position 2: `h = 3`, check if `citations[2] >= 3` ✓ (3 >= 3)  | 
 | 74 | +- Position 3: `h = 4`, check if `citations[3] >= 4` ✗ (1 < 4)  | 
 | 75 | + | 
 | 76 | +**Step 3: Find the maximum valid h**  | 
 | 77 | +- The largest h where `citations[h-1] >= h` is 3  | 
 | 78 | +- At position 2 (0-indexed), we have `h = 3` and `citations[2] = 3 >= 3`  | 
 | 79 | + | 
 | 80 | +**Step 4: Verify the h-index condition**  | 
 | 81 | +- We need at least 3 papers with at least 3 citations  | 
 | 82 | +- Papers with ≥3 citations: 6, 5, 3 (3 papers) ✓  | 
 | 83 | +- Remaining papers: 1, 0 (≤3 citations) ✓  | 
 | 84 | +- H-index is 3  | 
 | 85 | + | 
 | 86 | +**Why this works:**  | 
 | 87 | +After sorting in descending order, the array position `i` (0-indexed) represents `h = i + 1`. For a position to be a valid h-index, we need `citations[i] >= h`. The largest valid h is our answer.  | 
 | 88 | + | 
 | 89 | +> **Note:** The key insight is that after sorting, we can directly check each position as a potential h-index. The sorting makes it easy to verify the h-index condition in a single pass.  | 
 | 90 | +
  | 
 | 91 | +**Time Complexity:** O(n log n) - dominated by sorting the array    | 
 | 92 | +**Space Complexity:** O(1) - we only use a constant amount of extra space (excluding the sorted array if we modify the input)  | 
0 commit comments