File tree 1 file changed +7
-13
lines changed
1 file changed +7
-13
lines changed Original file line number Diff line number Diff line change @@ -4,24 +4,18 @@ pub fn trap(height: Vec<i32>) -> i32 {
4
4
let mut left = 0 ;
5
5
let mut right = height. len ( ) - 1 ;
6
6
let mut max_capacity = 0 ;
7
- let mut left_max = 0 ;
8
- let mut right_max = 0 ;
7
+ let mut left_max = height [ left ] ;
8
+ let mut right_max = height [ right ] ;
9
9
10
10
while left < right {
11
- if height[ left] < height[ right] {
12
- if height[ left] >= left_max {
13
- left_max = height[ left] ;
14
- } else {
15
- max_capacity += left_max - height[ left] ;
16
- }
11
+ if left_max < right_max {
17
12
left += 1 ;
13
+ left_max = left_max. max ( height[ left] ) ;
14
+ max_capacity += left_max - height[ left] ;
18
15
} else {
19
- if height[ right] >= right_max {
20
- right_max = height[ right] ;
21
- } else {
22
- max_capacity += right_max - height[ right] ;
23
- }
24
16
right -= 1 ;
17
+ right_max = right_max. max ( height[ right] ) ;
18
+ max_capacity += right_max - height[ right] ;
25
19
}
26
20
}
27
21
You can’t perform that action at this time.
0 commit comments