Refactor dynamic programming approach of Trapping Rain Water problem.

This commit is contained in:
Oleksii Trekhleb 2018-07-27 14:06:58 +03:00
parent f07e96ec59
commit 26239f9a97

View File

@ -74,7 +74,7 @@ the sides minus its own height.
- Initialize `answer = 0` - Initialize `answer = 0`
- Iterate the array from left to right: - Iterate the array from left to right:
- Initialize `max_left = 0 and `max_right = 0` - Initialize `max_left = 0` and `max_right = 0`
- Iterate from the current element to the beginning of array updating: `max_left = max(max_left, height[j])` - Iterate from the current element to the beginning of array updating: `max_left = max(max_left, height[j])`
- Iterate from the current element to the end of array updating: `max_right = max(max_right, height[j])` - Iterate from the current element to the end of array updating: `max_right = max(max_right, height[j])`
- Add `min(max_left, max_right) height[i]` to `answer` - Add `min(max_left, max_right) height[i]` to `answer`