@@ -4,29 +4,27 @@ LeetCode problem: [42. Trapping Rain Water](https://leetcode.com/problems/trappi
44## LeetCode problem description
55Given ` n ` non-negative integers representing an elevation map where the width of each bar is ` 1 ` , compute how much water it can trap after raining.
66
7+ ### Example 1
8+ ![ ] ( ../images/examples/0042_1.png )
79```
8- -----------------------------------------------------------------------------------------------------------
9- [Example 1]
10-
1110Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
1211Output: 6
1312
1413Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1].
1514In this case, 6 units of rain water (blue section) are being trapped.
16- -----------------------------------------------------------------------------------------------------------
17- [Example 2]
15+ ```
1816
17+ ### Example 2
18+ ```
1919Input: height = [4,2,0,3,2,5]
2020Output: 9
21- -----------------------------------------------------------------------------------------------------------
22- [Constraints]
23-
24- n == height.length
25- 1 <= n <= 2 * 10000
26- 0 <= height[i] <= 100000
27- -----------------------------------------------------------------------------------------------------------
2821```
2922
23+ ### Constraints
24+ - ` n == height.length `
25+ - ` 1 <= n <= 2 * 10000 `
26+ - ` 0 <= height[i] <= 100000 `
27+
3028## Thoughts
3129This problem can be solved using ** Monotonic Stack** .
3230
@@ -180,25 +178,32 @@ var trap = function (heights) {
180178
181179## C#
182180``` c#
183- public class Solution {
184- public int Trap (int [] heights ) {
185- var result = 0 ;
181+ public class Solution
182+ {
183+ public int Trap (int [] heights )
184+ {
185+ int result = 0 ;
186186 var indexStack = new Stack <int >();
187187
188- for (var i = 0 ; i < heights .Length ; i ++ ) {
189- var previousHeight = 0 ;
188+ for (var i = 0 ; i < heights .Length ; i ++ )
189+ {
190+ int previousHeight = 0 ;
190191
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 ;
192+ // situation 1: right side (i) is no shorter
193+ while (indexStack .Count > 0 && heights [indexStack .Peek ()] <= heights [i ])
194+ {
195+ int leftIndex = indexStack .Pop ();
196+ int heightGap = heights [leftIndex ] - previousHeight ;
197+ int width = i - leftIndex - 1 ;
195198 result += heightGap * width ;
196199 previousHeight = heights [leftIndex ];
197200 }
198201
199- if (indexStack .Count > 0 ) { // situation 2: right side (i) is shorter
200- var heightGap = heights [i ] - previousHeight ;
201- var width = i - indexStack .Peek () - 1 ;
202+ // situation 2: right side (i) is shorter
203+ if (indexStack .Count > 0 )
204+ {
205+ int heightGap = heights [i ] - previousHeight ;
206+ int width = i - indexStack .Peek () - 1 ;
202207 result += heightGap * width ;
203208 }
204209
0 commit comments