Skip to content

Commit 43b520f

Browse files
authored
Merge pull request Dijkstra-Edu#93 from AmanDeol7/aman/sol
Solution #274 - Aman Deol - 02/09/2025
2 parents 0d475c1 + e1223c0 commit 43b520f

File tree

4 files changed

+267
-0
lines changed

4 files changed

+267
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# H-Index
2+
3+
**Difficulty:** Medium
4+
**Category:** Arrays, Sorting, Counting Sort
5+
**LeetCode Link:** [Problem Link](https://leetcode.com/problems/h-index/)
6+
7+
---
8+
9+
## 📝 Introduction
10+
11+
We are given an array `citations` where `citations[i]` represents the number of citations a researcher received for their `i-th` paper.
12+
13+
The **H-Index** is defined as the **maximum value `h`** such that the researcher has published at least `h` papers that have each been cited at least `h` times.
14+
15+
We need to compute and return this H-Index.
16+
17+
---
18+
19+
## 💡 Approach & Key Insights
20+
21+
1. **Definition Recap**:
22+
The H-Index is the point where the number of papers with at least `h` citations is **greater than or equal to `h`**.
23+
24+
2. **Sorting Insight**:
25+
If we sort the `citations` in descending order, then the `h-index` is simply the largest index `i` where:
26+
```
27+
citations[i] >= i + 1
28+
```
29+
(`i+1` represents the number of papers considered so far).
30+
31+
3. **Alternative Counting Approach**:
32+
Since the maximum H-Index is at most `n` (number of papers), we can use a counting array to bucket citation frequencies and avoid sorting.
33+
34+
---
35+
36+
## 🛠️ Breakdown of Approaches
37+
38+
### 1️⃣ Sorting-Based Approach
39+
40+
- **Explanation:**
41+
- Sort citations in descending order.
42+
- Traverse and find the last index `i` such that `citations[i] >= i+1`.
43+
- That `i+1` is the H-Index.
44+
- **Time Complexity:** O(n log n) (due to sorting).
45+
- **Space Complexity:** O(1) (if in-place sort is allowed).
46+
47+
---
48+
49+
### 2️⃣ Counting Approach (Optimized)
50+
51+
- **Explanation:**
52+
- Create a frequency array `count` of size `n+1`.
53+
- For each citation `c`:
54+
- If `c >= n`, increment `count[n]` (since H-Index cannot exceed `n`).
55+
- Else increment `count[c]`.
56+
- Traverse `count` from `n` down to `0`, maintaining a cumulative sum of papers.
57+
- The first index `i` where cumulative papers ≥ `i` is the H-Index.
58+
- **Time Complexity:** O(n).
59+
- **Space Complexity:** O(n).
60+
61+
---
62+
63+
## 📊 Complexity Analysis
64+
65+
| Approach | Time Complexity | Space Complexity |
66+
| --------------------- | --------------- | ---------------- |
67+
| Sorting | O(n log n) | O(1) / O(n) |
68+
| Counting (Optimized) | O(n) | O(n) |
69+
70+
---
71+
72+
## 📌 Example Walkthroughs & Dry Runs
73+
74+
Example 1:
75+
```
76+
Input: citations = [3, 0, 6, 1, 5]
77+
78+
Step 1: Sort descending → [6, 5, 3, 1, 0]
79+
Step 2: Check each index:
80+
i=0 → 6 >= 1 → valid
81+
i=1 → 5 >= 2 → valid
82+
i=2 → 3 >= 3 → valid
83+
i=3 → 1 >= 4 → false → stop
84+
Answer = 3
85+
```
86+
87+
Output: `3`
88+
89+
---
90+
91+
Example 2:
92+
```
93+
Input: citations = [1, 3, 1]
94+
95+
Sort descending → [3, 1, 1]
96+
Check:
97+
i=0 → 3 >= 1 → valid
98+
i=1 → 1 >= 2 → false → stop
99+
Answer = 1
100+
```
101+
102+
Output: `1`
103+
104+
---
105+
106+
## 🔗 Additional Resources
107+
108+
- [H-Index Explanation (Wikipedia)](https://en.wikipedia.org/wiki/H-index)
109+
- [Leetcode Discuss - H-Index Solutions](https://leetcode.com/problems/h-index/discuss/)
110+
111+
---
112+
113+
Author: Aman Deol
114+
Date: 26/08/2025
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Sorting approach
2+
class Solution {
3+
public:
4+
int hIndex(vector<int>& citations) {
5+
sort(citations.rbegin(), citations.rend());
6+
for (int i = 0; i < citations.size(); i++) {
7+
if (citations[i] < i + 1)
8+
return i;
9+
}
10+
return citations.size();
11+
}
12+
};
13+
14+
// Counting approach
15+
class Solution {
16+
public:
17+
int hIndex(vector<int>& citations) {
18+
int n = citations.size();
19+
vector<int> count(n + 1, 0);
20+
for (int c : citations) {
21+
count[min(c, n)]++;
22+
}
23+
int total = 0;
24+
for (int i = n; i >= 0; i--) {
25+
total += count[i];
26+
if (total >= i)
27+
return i;
28+
}
29+
return 0;
30+
}
31+
};
32+
33+
// Sorting approach
34+
class Solution {
35+
public:
36+
int hIndex(vector<int>& citations) {
37+
sort(citations.rbegin(), citations.rend());
38+
for (int i = 0; i < citations.size(); i++) {
39+
if (citations[i] < i + 1)
40+
return i;
41+
}
42+
return citations.size();
43+
}
44+
};
45+
46+
// Counting approach
47+
class Solution {
48+
public:
49+
int hIndex(vector<int>& citations) {
50+
int n = citations.size();
51+
vector<int> count(n + 1, 0);
52+
for (int c : citations) {
53+
count[min(c, n)]++;
54+
}
55+
int total = 0;
56+
for (int i = n; i >= 0; i--) {
57+
total += count[i];
58+
if (total >= i)
59+
return i;
60+
}
61+
return 0;
62+
}
63+
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Sorting approach
2+
class Solution {
3+
public int hIndex(int[] citations) {
4+
Arrays.sort(citations);
5+
int n = citations.length;
6+
for (int i = 0; i < n; i++) {
7+
int h = n - i;
8+
if (citations[i] >= h) {
9+
return h;
10+
}
11+
}
12+
return 0;
13+
}
14+
}
15+
16+
// Counting approach
17+
class Solution {
18+
public int hIndex(int[] citations) {
19+
int n = citations.length;
20+
int[] count = new int[n + 1];
21+
for (int c : citations) {
22+
count[Math.min(c, n)]++;
23+
}
24+
int total = 0;
25+
for (int i = n; i >= 0; i--) {
26+
total += count[i];
27+
if (total >= i) {
28+
return i;
29+
}
30+
}
31+
return 0;
32+
}
33+
}
34+
35+
// Sorting approach
36+
class Solution {
37+
public int hIndex(int[] citations) {
38+
Arrays.sort(citations);
39+
int n = citations.length;
40+
for (int i = 0; i < n; i++) {
41+
int h = n - i;
42+
if (citations[i] >= h) {
43+
return h;
44+
}
45+
}
46+
return 0;
47+
}
48+
}
49+
50+
// Counting approach
51+
class Solution {
52+
public int hIndex(int[] citations) {
53+
int n = citations.length;
54+
int[] count = new int[n + 1];
55+
for (int c : citations) {
56+
count[Math.min(c, n)]++;
57+
}
58+
int total = 0;
59+
for (int i = n; i >= 0; i--) {
60+
total += count[i];
61+
if (total >= i) {
62+
return i;
63+
}
64+
}
65+
return 0;
66+
}
67+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Sorting approach
2+
class Solution:
3+
def hIndex(self, citations: List[int]) -> int:
4+
citations.sort(reverse=True)
5+
for i, c in enumerate(citations):
6+
if c < i + 1:
7+
return i
8+
return len(citations)
9+
10+
# Counting approach
11+
class Solution:
12+
def hIndex(self, citations: List[int]) -> int:
13+
n = len(citations)
14+
count = [0] * (n + 1)
15+
for c in citations:
16+
count[min(c, n)] += 1
17+
18+
total = 0
19+
for i in range(n, -1, -1):
20+
total += count[i]
21+
if total >= i:
22+
return i
23+
return 0

0 commit comments

Comments
 (0)