forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0042-trapping-rain-water.rs
71 lines (64 loc) · 1.76 KB
/
0042-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
/**
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
impl Solution {
pub fn trap(height: Vec<i32>) -> i32 {
if height.is_empty() {
return 0;
}
let (mut left, mut right) = (0, height.len() - 1);
let (mut left_max, mut right_max) = (height[left], height[right]);
let mut result = 0;
while left < right {
if left_max < right_max {
left += 1;
left_max = left_max.max(height[left]);
result += left_max - height[left];
} else {
right-= 1;
right_max = right_max.max(height[right]);
result += right_max - height[right];
}
}
result
}
}
/**
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
impl Solution {
pub fn trap(height: Vec<i32>) -> i32 {
let mut max_left = vec![];
let mut max_right = vec![];
let mut min_left_right = vec![];
let mut current = 0;
for (_, val) in height.iter().enumerate() {
max_left.push(current);
current = current.max(*val);
}
let mut current = 0;
for (_, val) in height.iter().enumerate().rev() {
max_right.push(current);
current = current.max(*val);
}
max_right.reverse();
min_left_right = max_left
.iter()
.zip(max_right.iter())
.map(|x| x.0.min(x.1))
.collect();
min_left_right
.iter()
.enumerate()
.map(|x| {
if *x.1 - height[x.0] > 0 {
return *x.1 - height[x.0];
} else {
return 0;
}
})
.sum()
}
}