|
| 1 | +--- |
| 2 | +id: h-index |
| 3 | +title: H-Index (LeetCode) |
| 4 | +sidebar_label: 0274-H-Index |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Sorting |
| 8 | + - Counting Sort |
| 9 | +description: Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return the researcher's h-index. |
| 10 | +sidebar_position: 0274 |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, 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. |
| 16 | + |
| 17 | +### Example 1 |
| 18 | + |
| 19 | +- **Input:** ` citations = [3,0,6,1,5]` |
| 20 | +- **Output:** `3` |
| 21 | +- **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. |
| 22 | +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. |
| 23 | + |
| 24 | +### Example 2 |
| 25 | + |
| 26 | +- **Input:** `citations = [1,3,1] ` |
| 27 | +- **Output:** `1` |
| 28 | + |
| 29 | +### Constraints |
| 30 | + |
| 31 | +- `n == citations.length` |
| 32 | +- `1 <= n <= 5000` |
| 33 | +- `0 <= citations[i] <= 1000` |
| 34 | + |
| 35 | +## Approach |
| 36 | +Create a function hasLeastCitations with a parameter h to check if there are at least h papers with >= h citations. When hasLeastCitations(x) is true, hasLeastCitations(x-1) is also true. This means that hasLeastCitations is a monotonic function, so we can binary search for the highest h for which it return true. This h is our h-index. |
| 37 | + |
| 38 | +### Solution Code |
| 39 | + |
| 40 | +#### C++ |
| 41 | + |
| 42 | +```c++ |
| 43 | +class Solution { |
| 44 | +public: |
| 45 | + bool hasLeastCitations(int h, vector<int>& citations) { |
| 46 | + int count = 0; |
| 47 | + for (int cite_count : citations) { |
| 48 | + if (cite_count >= h) |
| 49 | + count++; |
| 50 | + } |
| 51 | + return count >= h; |
| 52 | + } |
| 53 | + int hIndex(vector<int>& citations) { |
| 54 | + int low = 0, high = citations.size(); |
| 55 | + while (low <= high) { |
| 56 | + int mid = (low + high) / 2; |
| 57 | + if (hasLeastCitations(mid, citations)) |
| 58 | + low = mid + 1; |
| 59 | + else |
| 60 | + high = mid - 1; |
| 61 | + } |
| 62 | + return high; |
| 63 | + } |
| 64 | +}; |
| 65 | +``` |
| 66 | +
|
| 67 | +#### Java |
| 68 | +```java |
| 69 | +class Solution { |
| 70 | + static boolean hasLeastCitations(int h, int[] citations) { |
| 71 | + int count = 0; |
| 72 | + for (int cite_count : citations) { |
| 73 | + if (cite_count >= h) |
| 74 | + count++; |
| 75 | + } |
| 76 | + return count >= h; |
| 77 | + } |
| 78 | + public int hIndex(int[] citations) { |
| 79 | + int low = 0, high = citations.length; |
| 80 | + while (low <= high) { |
| 81 | + int mid = (low + high) / 2; |
| 82 | + if (hasLeastCitations(mid, citations)) |
| 83 | + low = mid + 1; |
| 84 | + else |
| 85 | + high = mid - 1; |
| 86 | + } |
| 87 | + return high; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +#### Python |
| 93 | +```python |
| 94 | +class Solution: |
| 95 | + def hIndex(self, citations: List[int]) -> int: |
| 96 | + |
| 97 | + def hasLeastCitations(h, citations): |
| 98 | + return sum(cite_count >= h for cite_count in citations) >= h |
| 99 | + |
| 100 | + low = 0 |
| 101 | + high = len(citations) |
| 102 | + while low <= high: |
| 103 | + mid = (low + high) // 2 |
| 104 | + if hasLeastCitations(mid, citations): |
| 105 | + low = mid + 1 |
| 106 | + else: |
| 107 | + high = mid - 1 |
| 108 | + return high |
| 109 | + |
| 110 | +``` |
| 111 | + |
| 112 | +#### Conclusion |
| 113 | +- Time Complexity |
| 114 | + |
| 115 | + - 1. Sorting is O(nlogn) |
| 116 | + |
| 117 | + - 2. Looping is O(n) |
| 118 | + |
| 119 | + - The total time complexity as O(n log n). |
| 120 | + |
| 121 | +- Space Complexity |
| 122 | +The only memory we allocate is the integer h, so the space complexity is O(1). |
0 commit comments