Skip to content

Commit 93060b9

Browse files
committed
add: H-Index
1 parent 4ca054e commit 93060b9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

β€Ž0274-h-index.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn h_index(citations: Vec<i32>) -> i32 {
5+
let n = citations.len();
6+
let mut buckets = vec![0; n + 1];
7+
for c in citations {
8+
if c >= n as i32 {
9+
buckets[n] += 1;
10+
} else {
11+
buckets[c as usize] += 1;
12+
}
13+
}
14+
15+
let mut total = 0;
16+
for (i, count) in buckets.into_iter().enumerate().rev() {
17+
total += count;
18+
if total >= i as i32 {
19+
return i as i32;
20+
}
21+
}
22+
0
23+
}
24+
}
25+
26+
fn main() {
27+
assert_eq!(3, Solution::h_index(vec![0, 3, 6, 1, 5]));
28+
assert_eq!(3, Solution::h_index(vec![3, 3, 25, 8, 5]));
29+
assert_eq!(2, Solution::h_index(vec![0, 5, 2, 6, 1]));
30+
assert_eq!(1, Solution::h_index(vec![0, 1]));
31+
assert_eq!(0, Solution::h_index(vec![0, 0]));
32+
assert_eq!(1, Solution::h_index(vec![1, 1]));
33+
assert_eq!(1, Solution::h_index(vec![1, 1, 1]));
34+
assert_eq!(1, Solution::h_index(vec![100]));
35+
assert_eq!(0, Solution::h_index(vec![]));
36+
}

0 commit comments

Comments
 (0)