-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path42.trapping-rain-water.rs
82 lines (65 loc) · 1.82 KB
/
42.trapping-rain-water.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
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* @lc app=leetcode id=42 lang=rust
*
* [42] Trapping Rain Water
*/
// @lc code=start
use std::cmp;
impl Solution {
pub fn trap(height: Vec<i32>) -> i32 {
// Self::trap_two_way(height)
Self::trap_two_pointer(height)
}
pub fn trap_two_way(height: Vec<i32>) -> i32 {
let N = height.len();
if N <= 2 { return 0; }
let mut max_from_left = vec![0];
let mut max_from_right = vec![0];
for i in 1..N {
max_from_left.push(
cmp::max(*max_from_left.last().unwrap(), height[i-1])
);
}
for i in (0..N-1).rev() {
max_from_right.push(
cmp::max(*max_from_right.last().unwrap(), height[i+1])
);
}
max_from_right.reverse();
let mut ans = 0;
for i in 1..N-1 {
ans += cmp::max(
cmp::min(max_from_left[i], max_from_right[i]) - height[i],
0
)
}
ans
}
pub fn trap_two_pointer(height: Vec<i32>) -> i32 {
if height.len() <= 2 { return 0; }
let mut left = 0;
let mut right = height.len() - 1;
let mut ans = 0;
let mut maxleft = 0;
let mut maxright = 0;
while left <= right {
if height[left] <= height[right] {
if maxleft < height[left] {
maxleft = height[left];
} else {
ans += maxleft - height[left];
}
left += 1;
} else {
if maxright < height[right] {
maxright = height[right];
} else {
ans += maxright - height[right];
}
right -= 1;
}
}
ans
}
}
// @lc code=end