Skip to content

Commit b51b8aa

Browse files
committed
0042-trapping-rain-water.md C# formatted; Added image of example; Perfected examples.
1 parent 6c0faa3 commit b51b8aa

3 files changed

Lines changed: 55 additions & 47 deletions

File tree

images/examples/0042_1.png

7.66 KB
Loading

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,27 @@ LeetCode problem: [42. Trapping Rain Water](https://leetcode.com/problems/trappi
44
## LeetCode problem description
55
Given `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-
1110
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
1211
Output: 6
1312
1413
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1].
1514
In this case, 6 units of rain water (blue section) are being trapped.
16-
-----------------------------------------------------------------------------------------------------------
17-
[Example 2]
15+
```
1816

17+
### Example 2
18+
```
1919
Input: height = [4,2,0,3,2,5]
2020
Output: 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
3129
This problem can be solved using **Monotonic Stack**.
3230

@@ -167,23 +165,28 @@ var trap = function (heights) {
167165

168166
## C#
169167
```c#
170-
public class Solution {
171-
public int Trap(int[] heights) {
172-
var result = 0;
168+
public class Solution
169+
{
170+
public int Trap(int[] heights)
171+
{
172+
int result = 0;
173173
var indexStack = new Stack<int>();
174174

175-
for (var i = 0; i < heights.Length; i++) {
176-
while (indexStack.Count > 0 && heights[indexStack.Peek()] <= heights[i]) {
177-
var poppedIndex = indexStack.Pop();
175+
for (var i = 0; i < heights.Length; i++)
176+
{
177+
while (indexStack.Count > 0 && heights[indexStack.Peek()] <= heights[i])
178+
{
179+
int poppedIndex = indexStack.Pop();
178180

179-
if (indexStack.Count == 0) {
181+
if (indexStack.Count == 0)
182+
{
180183
break;
181184
}
182185

183-
var leftHeight = heights[indexStack.Peek()];
184-
var rightHeight = heights[i];
185-
var heightGap = Math.Min(leftHeight, rightHeight) - heights[poppedIndex];
186-
var width = i - indexStack.Peek() - 1;
186+
int leftHeight = heights[indexStack.Peek()];
187+
int rightHeight = heights[i];
188+
int heightGap = Math.Min(leftHeight, rightHeight) - heights[poppedIndex];
189+
int width = i - indexStack.Peek() - 1;
187190
result += heightGap * width;
188191
}
189192

problems/0042-trapping-rain-water.md

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,27 @@ LeetCode problem: [42. Trapping Rain Water](https://leetcode.com/problems/trappi
44
## LeetCode problem description
55
Given `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-
1110
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
1211
Output: 6
1312
1413
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1].
1514
In this case, 6 units of rain water (blue section) are being trapped.
16-
-----------------------------------------------------------------------------------------------------------
17-
[Example 2]
15+
```
1816

17+
### Example 2
18+
```
1919
Input: height = [4,2,0,3,2,5]
2020
Output: 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
3129
This 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

Comments
 (0)