-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrapping_rain_water.rs
34 lines (32 loc) · 1.25 KB
/
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
impl Solution {
pub fn trap(height: Vec<i32>) -> i32 {
let (mut left, mut right) = (0, height.len() - 1);
let (mut left_max, mut right_max) = (0, 0);
let mut ans = 0;
// iterate from both sides to the middle
while left < right {
// if left is lower than right, then the water level depends on left
// else the water level depends on right
if height[left] < height[right] {
// if left height is higher than left_max, then update left_max
// else add the difference between left_max and left height to ans
if height[left] >= left_max {
left_max = height[left];
} else {
ans += left_max - height[left];
}
left += 1;
} else {
// if right height is higher than right_max, then update right_max
// else add the difference between right_max and right height to ans
if height[right] >= right_max {
right_max = height[right];
} else {
ans += right_max - height[right];
}
right -= 1;
}
}
ans
}
}