Skip to content

Commit 81e4e4b

Browse files
committed
Added images for 0042-trapping-rain-water.md
1 parent 38c6280 commit 81e4e4b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

images/0042.png

128 KB
Loading

problems/0042-trapping-rain-water.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,23 @@ n == height.length
3030
## Thoughts
3131
This problem can be solved using **Monotonic Stack**.
3232

33-
### Rules:
33+
### Solution 1
34+
#### Rules
3435
* Whenever there are area (rain) can be added, add it immediately.
3536
* The shorter side's height will be used to calculate the area.
3637
* There would be two situations:
3738
1. The left side (the top of stack) is taller than the right side (current item).
3839
2. The left side (the top of stack) is not taller than the right side (current item).
3940

41+
![](../images/0042.png)
42+
4043
Detailed solutions will be given later, and now only the best practices in 7 languages are given.
4144

45+
### Solution 2
46+
The `solution 2` will follow **Monotonic Stack**'s common rule: **only calculating when `pop()` is happening**.
47+
48+
Please click [42. Trapping Rain Water (solution 2)](./0042-trapping-rain-water-2.md) to see it.
49+
4250
### Complexity
4351
* Time: `O(n)`.
4452
* Space: `O(n)`.
@@ -102,6 +110,8 @@ class Solution:
102110
return result
103111
```
104112

113+
![](../images/0042.png)
114+
105115
## C++
106116
```cpp
107117
class Solution {
@@ -166,6 +176,8 @@ var trap = function (heights) {
166176
};
167177
```
168178

179+
![](../images/0042.png)
180+
169181
## C#
170182
```c#
171183
public class Solution {
@@ -229,6 +241,8 @@ func trap(heights []int) int {
229241
}
230242
```
231243

244+
![](../images/0042.png)
245+
232246
## Ruby
233247
```ruby
234248
def trap(heights = [])

0 commit comments

Comments
 (0)