File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed
Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -30,15 +30,23 @@ n == height.length
3030## Thoughts
3131This 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+
4043Detailed 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
107117class Solution {
@@ -166,6 +176,8 @@ var trap = function (heights) {
166176};
167177```
168178
179+ ![ ] ( ../images/0042.png )
180+
169181## C#
170182``` c#
171183public class Solution {
@@ -229,6 +241,8 @@ func trap(heights []int) int {
229241}
230242```
231243
244+ ![ ] ( ../images/0042.png )
245+
232246## Ruby
233247``` ruby
234248def trap (heights = [])
You can’t perform that action at this time.
0 commit comments