Skip to content

Commit 38e0c03

Browse files
committed
0042-trapping-rain-water.md Renamed code and perfected comments.
1 parent 97f1f4a commit 38e0c03

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

problems/0042-trapping-rain-water.md

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ This problem can be solved using **Monotonic Stack**.
3434
#### Rules
3535
* Whenever there are area (rain) can be added, add it immediately.
3636
* The shorter side's height will be used to calculate the area.
37-
* There would be two situations:
38-
1. The left side (the top of stack) is taller than the right side (current item).
39-
2. The left side (the top of stack) is not taller than the right side (current item).
37+
* There would be **two situations**:
38+
1) The right side (current item) is **no shorter** than the left side (the top of stack).
39+
2) The right side (current item) is **shorter** than the left side (the top of stack).
4040

4141
![](../images/0042.png)
4242

@@ -61,15 +61,15 @@ class Solution {
6161
for (var i = 0; i < heights.length; i++) {
6262
var previousHeight = 0;
6363

64-
while (!indexStack.empty() && heights[indexStack.peek()] < heights[i]) { // situation 2
65-
var index = indexStack.pop();
66-
var heightGap = heights[index] - previousHeight;
67-
var width = i - index - 1;
64+
while (!indexStack.empty() && heights[indexStack.peek()] <= heights[i]) { // situation 1: right side (i) is no shorter
65+
var leftIndex = indexStack.pop();
66+
var heightGap = heights[leftIndex] - previousHeight;
67+
var width = i - leftIndex - 1;
6868
result += heightGap * width;
69-
previousHeight = heights[index];
69+
previousHeight = heights[leftIndex];
7070
}
7171

72-
if (!indexStack.empty()) { // situation 1
72+
if (!indexStack.empty()) { // situation 2: right side (i) is shorter
7373
var heightGap = heights[i] - previousHeight;
7474
var width = i - indexStack.peek() - 1;
7575
result += heightGap * width;
@@ -93,14 +93,14 @@ class Solution:
9393
for i, height in enumerate(heights):
9494
previous_height = 0
9595

96-
while index_stack and heights[index_stack[-1]] <= height: # situation 2
97-
index = index_stack.pop()
98-
height_gap = heights[index] - previous_height
99-
width = i - index - 1
96+
while index_stack and heights[index_stack[-1]] <= height: # situation 1: right side (i) is no shorter
97+
left_index = index_stack.pop()
98+
height_gap = heights[left_index] - previous_height
99+
width = i - left_index - 1
100100
result += height_gap * width
101-
previous_height = heights[index]
101+
previous_height = heights[left_index]
102102

103-
if index_stack: # situation 1
103+
if index_stack: # situation 2: right side (i) is shorter
104104
height_gap = height - previous_height
105105
width = i - index_stack[-1] - 1
106106
result += height_gap * width
@@ -123,16 +123,16 @@ public:
123123
for (auto i = 0; i < heights.size(); i++) {
124124
auto previous_height = 0;
125125

126-
while (!index_stack.empty() && heights[index_stack.top()] < heights[i]) { // situation 2
127-
auto index = index_stack.top();
128-
auto height_gap = heights[index] - previous_height;
129-
auto width = i - index - 1;
130-
result += height_gap * width;
131-
previous_height = heights[index];
126+
while (!index_stack.empty() && heights[index_stack.top()] <= heights[i]) { // situation 1: right side (i) is no shorter
127+
auto leftIndex = index_stack.top();
132128
index_stack.pop();
129+
auto height_gap = heights[leftIndex] - previous_height;
130+
auto width = i - leftIndex - 1;
131+
result += height_gap * width;
132+
previous_height = heights[leftIndex];
133133
}
134134

135-
if (!index_stack.empty()) { // situation 1
135+
if (!index_stack.empty()) { // situation 2: right side (i) is shorter
136136
auto height_gap = heights[i] - previous_height;
137137
auto width = i - index_stack.top() - 1;
138138
result += height_gap * width;
@@ -155,15 +155,15 @@ var trap = function (heights) {
155155
heights.forEach((height, i) => {
156156
let previousHeight = 0
157157

158-
while (indexStack.length > 0 && heights[indexStack.at(-1)] < height) { // situation 2
159-
const index = indexStack.pop()
160-
const heightGap = heights[index] - previousHeight
161-
const width = i - index - 1
158+
while (indexStack.length > 0 && heights[indexStack.at(-1)] <= height) { // situation 1: right side (i) is no shorter
159+
const leftIndex = indexStack.pop()
160+
const heightGap = heights[leftIndex] - previousHeight
161+
const width = i - leftIndex - 1
162162
result += heightGap * width
163-
previousHeight = heights[index]
163+
previousHeight = heights[leftIndex]
164164
}
165165

166-
if (indexStack.length > 0) { // situation 1
166+
if (indexStack.length > 0) { // situation 2: right side (i) is shorter
167167
const heightGap = height - previousHeight
168168
const width = i - indexStack.at(-1) - 1
169169
result += heightGap * width
@@ -188,15 +188,15 @@ public class Solution {
188188
for (var i = 0; i < heights.Length; i++) {
189189
var previousHeight = 0;
190190

191-
while (indexStack.Count > 0 && heights[indexStack.Peek()] < heights[i]) { // situation 2
192-
var index = indexStack.Pop();
193-
var heightGap = heights[index] - previousHeight;
194-
var width = i - index - 1;
191+
while (indexStack.Count > 0 && heights[indexStack.Peek()] <= heights[i]) { // situation 1: right side (i) is no shorter
192+
var leftIndex = indexStack.Pop();
193+
var heightGap = heights[leftIndex] - previousHeight;
194+
var width = i - leftIndex - 1;
195195
result += heightGap * width;
196-
previousHeight = heights[index];
196+
previousHeight = heights[leftIndex];
197197
}
198198

199-
if (indexStack.Count > 0) { // situation 1
199+
if (indexStack.Count > 0) { // situation 2: right side (i) is shorter
200200
var heightGap = heights[i] - previousHeight;
201201
var width = i - indexStack.Peek() - 1;
202202
result += heightGap * width;
@@ -219,16 +219,16 @@ func trap(heights []int) int {
219219
for i, height := range heights {
220220
previousHeight := 0
221221

222-
for len(indexStack) > 0 && heights[indexStack[len(indexStack) - 1]] < height { // situation 2
223-
index := indexStack[len(indexStack) - 1]
224-
heightGap := heights[index] - previousHeight
225-
width := i - index - 1
222+
for len(indexStack) > 0 && heights[indexStack[len(indexStack) - 1]] <= height { // situation 1: right side (i) is no shorter
223+
leftIndex := indexStack[len(indexStack) - 1]
224+
heightGap := heights[leftIndex] - previousHeight
225+
width := i - leftIndex - 1
226226
result += heightGap * width
227-
previousHeight = heights[index]
227+
previousHeight = heights[leftIndex]
228228
indexStack = indexStack[:len(indexStack) - 1]
229229
}
230230

231-
if len(indexStack) > 0 { // situation 1
231+
if len(indexStack) > 0 { // situation 2: right side (i) is shorter
232232
heightGap := height - previousHeight
233233
width := i - indexStack[len(indexStack) - 1] - 1
234234
result += heightGap * width
@@ -245,22 +245,23 @@ func trap(heights []int) int {
245245

246246
## Ruby
247247
```ruby
248+
# Original article at https://github.com/gazeldx/leetcode-best-practice
248249
def trap(heights = [])
249250
result = 0
250251
index_stack = []
251252

252253
heights.each_with_index do |height, i|
253254
previous_height = 0
254255

255-
while !index_stack.empty? && heights[index_stack[-1]] <= height # situation 2
256-
index = index_stack.pop
257-
height_gap = heights[index] - previous_height
258-
width = i - index - 1
256+
while !index_stack.empty? && heights[index_stack[-1]] <= height # situation 1: right side (i) is no shorter
257+
left_index = index_stack.pop
258+
height_gap = heights[left_index] - previous_height
259+
width = i - left_index - 1
259260
result += height_gap * width
260-
previous_height = heights[index]
261+
previous_height = heights[left_index]
261262
end
262263

263-
if !index_stack.empty? # situation 1
264+
if !index_stack.empty? # situation 2: right side (i) is shorter
264265
height_gap = height - previous_height
265266
width = i - index_stack[-1] - 1
266267
result += height_gap * width

0 commit comments

Comments
 (0)