Skip to content

Commit 5188ac2

Browse files
committed
Perfected code by using <=; Perfected image.
1 parent 38e0c03 commit 5188ac2

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

images/0042.png

94.5 KB
Loading

problems/0042-trapping-rain-water-2.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Solution {
5454
var indexStack = new Stack<Integer>();
5555

5656
for (var i = 0; i < heights.length; i++) {
57-
while (!indexStack.empty() && heights[indexStack.peek()] < heights[i]) {
57+
while (!indexStack.empty() && heights[indexStack.peek()] <= heights[i]) {
5858
var poppedIndex = indexStack.pop();
5959

6060
if (indexStack.empty()) {
@@ -84,7 +84,7 @@ class Solution:
8484
index_stack = []
8585

8686
for i, height in enumerate(heights):
87-
while index_stack and heights[index_stack[-1]] < height:
87+
while index_stack and heights[index_stack[-1]] <= height:
8888
popped_index = index_stack.pop()
8989

9090
if not index_stack:
@@ -114,7 +114,7 @@ public:
114114
for (auto i = 0; i < heights.size(); i++) {
115115
auto previous_height = 0;
116116

117-
while (!index_stack.empty() && heights[index_stack.top()] < heights[i]) {
117+
while (!index_stack.empty() && heights[index_stack.top()] <= heights[i]) {
118118
auto popped_index = index_stack.top();
119119
index_stack.pop();
120120

@@ -144,7 +144,7 @@ var trap = function (heights) {
144144
const indexStack = []
145145

146146
heights.forEach((height, i) => {
147-
while (indexStack.length > 0 && heights[indexStack.at(-1)] < height) {
147+
while (indexStack.length > 0 && heights[indexStack.at(-1)] <= height) {
148148
const poppedIndex = indexStack.pop()
149149

150150
if (indexStack.length === 0) {
@@ -175,7 +175,7 @@ public class Solution {
175175
var indexStack = new Stack<int>();
176176

177177
for (var i = 0; i < heights.Length; i++) {
178-
while (indexStack.Count > 0 && heights[indexStack.Peek()] < heights[i]) {
178+
while (indexStack.Count > 0 && heights[indexStack.Peek()] <= heights[i]) {
179179
var poppedIndex = indexStack.Pop();
180180

181181
if (indexStack.Count == 0) {
@@ -204,7 +204,7 @@ func trap(heights []int) int {
204204
indexStack := []int{}
205205

206206
for i, height := range heights {
207-
for len(indexStack) > 0 && heights[indexStack[len(indexStack) - 1]] < height {
207+
for len(indexStack) > 0 && heights[indexStack[len(indexStack) - 1]] <= height {
208208
poppedIndex := indexStack[len(indexStack) - 1]
209209
indexStack = indexStack[:len(indexStack) - 1]
210210

problems/0042-trapping-rain-water.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ public:
124124
auto previous_height = 0;
125125

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

135135
if (!index_stack.empty()) { // situation 2: right side (i) is shorter

0 commit comments

Comments
 (0)