Skip to content

Commit 81d1d6e

Browse files
committed
refactor: eliminate some if innecesary
1 parent 85ec018 commit 81d1d6e

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/hard/trapping_rain_water.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@ pub fn trap(height: Vec<i32>) -> i32 {
44
let mut left = 0;
55
let mut right = height.len() - 1;
66
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];
99

1010
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 {
1712
left += 1;
13+
left_max = left_max.max(height[left]);
14+
max_capacity += left_max - height[left];
1815
} else {
19-
if height[right] >= right_max {
20-
right_max = height[right];
21-
} else {
22-
max_capacity += right_max - height[right];
23-
}
2416
right -= 1;
17+
right_max = right_max.max(height[right]);
18+
max_capacity += right_max - height[right];
2519
}
2620
}
2721

0 commit comments

Comments
 (0)