-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathh_index.rs
More file actions
39 lines (34 loc) · 1.1 KB
/
h_index.rs
File metadata and controls
39 lines (34 loc) · 1.1 KB
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
37
38
39
// # H-Index
// 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.
pub fn solution(mut citations: Vec<i32>) -> i32 {
citations.sort();
let papers_count = citations.len();
for h_candidate in 0..papers_count {
if (papers_count - h_candidate) as i32 <= citations[h_candidate] {
return (papers_count - h_candidate) as i32;
}
}
0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example_1() {
let citations = vec![3, 0, 6, 1, 5];
assert_eq!(solution(citations), 3);
}
#[test]
fn example_2() {
let citations = vec![1, 3, 1];
assert_eq!(solution(citations), 1);
}
#[test]
fn example_3() {
let citations = vec![0, 0, 0];
assert_eq!(solution(citations), 0);
}
}