-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp275.rs
52 lines (47 loc) · 1.42 KB
/
p275.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#[test]
fn test() {
use method2::h_index;
// assert_eq!(h_index(vec![0, 1, 3, 5, 6]), 3);
// assert_eq!(h_index(vec![1, 2, 100]), 2);
assert_eq!(h_index(vec![0]), 0);
}
// 与p274类似,但是citations是有序的
// 时间复杂度:O(n)
#[allow(unused)]
mod method1 {
pub fn h_index(citations: Vec<i32>) -> i32 {
// 引用次数>h_index,h_index+1
let mut h_index: i32 = 0;
for &citation in citations.iter().rev() {
if citation > h_index {
h_index += 1;
}
}
h_index
}
}
// 二分查找可以降低时间复杂度
// 时间复杂度:O(log(n))
// 设数组长度为n
// 每次二分可以知道,mid右侧的论文(共n-mid篇)至少被引用了citations[mid]次
// 如果citations[mid]>=n-mid,说明可以往右扩展多包括几篇文献,h index可能会更大,移动right
// 否则,移动left
// 二分的关键还是边界问题
#[allow(unused)]
mod method2 {
pub fn h_index(citations: Vec<i32>) -> i32 {
let n: usize = citations.len();
let mut left: usize = 0;
let mut right: usize = n;
// [left, right)
while left < right {
let mid: usize = left + (right - left) / 2;
if citations[mid] >= (n - mid) as i32 {
right = mid;
} else {
left = mid + 1;
}
}
(n - left) as i32
}
}