Skip to content

Commit 1737b21

Browse files
committed
Added h-index
1 parent 12b4698 commit 1737b21

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <vector>
2+
#include <algorithm>
3+
4+
class Solution {
5+
public:
6+
int hIndex(std::vector<int>& citations) {
7+
int n = citations.size();
8+
std::vector<int> paperCounts(n + 1, 0);
9+
10+
for (int c : citations) {
11+
paperCounts[std::min(n, c)]++;
12+
}
13+
14+
int h = n;
15+
int papers = paperCounts[n];
16+
17+
while (papers < h) {
18+
h--;
19+
papers += paperCounts[h];
20+
}
21+
22+
return h;
23+
}
24+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Solution {
2+
public int hIndex(int[] citations) {
3+
int n = citations.length;
4+
int[] paperCounts = new int[n + 1];
5+
6+
for (int c : citations) {
7+
paperCounts[Math.min(n, c)]++;
8+
}
9+
10+
int h = n;
11+
int papers = paperCounts[n];
12+
13+
while (papers < h) {
14+
h--;
15+
papers += paperCounts[h];
16+
}
17+
18+
return h;
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var hIndex = function(citations) {
2+
var n = citations.length;
3+
var paperCounts = new Array(n + 1).fill(0);
4+
5+
for (var c of citations) {
6+
paperCounts[Math.min(n, c)]++;
7+
}
8+
9+
var h = n;
10+
var papers = paperCounts[n];
11+
12+
while (papers < h) {
13+
h--;
14+
papers += paperCounts[h];
15+
}
16+
17+
return h;
18+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def hIndex(self, citations: List[int]) -> int:
3+
n = len(citations)
4+
paper_counts = [0] * (n+1)
5+
6+
for c in citations:
7+
paper_counts[min(n, c)] += 1
8+
9+
h = n
10+
papers = paper_counts[n]
11+
12+
while papers < h:
13+
h -= 1
14+
papers += paper_counts[h]
15+
16+
return h
17+
# Time: O(n), Space: O(n)

0 commit comments

Comments
 (0)