-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp42.rs
68 lines (59 loc) · 1.75 KB
/
p42.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#[test]
fn test() {
use method2::trap;
assert_eq!(trap(vec![0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]), 6);
assert_eq!(trap(vec![4, 2, 0, 3, 2, 5]), 9);
}
// 前后缀分解,木桶能接多少水取决于前缀最大值和后缀最大值
// 时间复杂度 O(n)
// 空间复杂度 O(n)
mod method1 {
#[allow(unused)]
pub fn trap(height: Vec<i32>) -> i32 {
let n: usize = height.len();
// 前缀最大值
let mut pre_max: Vec<i32> = vec![0; n];
pre_max[0] = height[0];
for i in 1..n {
pre_max[i] = pre_max[i - 1].max(height[i]);
}
// 后缀最大值
let mut suf_max: Vec<i32> = vec![0; n];
suf_max[n - 1] = height[n - 1];
for i in (0..n - 1).rev() {
suf_max[i] = suf_max[i + 1].max(height[i]);
}
// 计算当前位置能接多少雨水
let mut ans: i32 = 0;
for i in 0..n {
ans += pre_max[i].min(suf_max[i]) - height[i];
}
ans
}
}
// 双指针
// 时间复杂度 O(n)
// 空间复杂度 O(1)
mod method2 {
#[allow(unused)]
pub fn trap(height: Vec<i32>) -> i32 {
let n: usize = height.len();
let mut ans: i32 = 0;
let mut left: i32 = 0;
let mut right: i32 = (n - 1) as i32;
let mut pre_max: i32 = 0;
let mut suf_max: i32 = 0;
while left <= right {
pre_max = pre_max.max(height[left as usize]);
suf_max = suf_max.max(height[right as usize]);
if pre_max < suf_max {
ans += pre_max - height[left as usize];
left += 1;
} else {
ans += suf_max - height[right as usize];
right -= 1;
}
}
ans
}
}