-
Notifications
You must be signed in to change notification settings - Fork 2
/
0274-h-index.rs
36 lines (33 loc) · 1.04 KB
/
0274-h-index.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
struct Solution;
impl Solution {
pub fn h_index(citations: Vec<i32>) -> i32 {
let n = citations.len();
let mut buckets = vec![0; n + 1];
for c in citations {
if c >= n as i32 {
buckets[n] += 1;
} else {
buckets[c as usize] += 1;
}
}
let mut total = 0;
for (i, count) in buckets.into_iter().enumerate().rev() {
total += count;
if total >= i as i32 {
return i as i32;
}
}
0
}
}
fn main() {
assert_eq!(3, Solution::h_index(vec![0, 3, 6, 1, 5]));
assert_eq!(3, Solution::h_index(vec![3, 3, 25, 8, 5]));
assert_eq!(2, Solution::h_index(vec![0, 5, 2, 6, 1]));
assert_eq!(1, Solution::h_index(vec![0, 1]));
assert_eq!(0, Solution::h_index(vec![0, 0]));
assert_eq!(1, Solution::h_index(vec![1, 1]));
assert_eq!(1, Solution::h_index(vec![1, 1, 1]));
assert_eq!(1, Solution::h_index(vec![100]));
assert_eq!(0, Solution::h_index(vec![]));
}