-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathTrappingRainWater.cs
executable file
·66 lines (57 loc) · 3.45 KB
/
TrappingRainWater.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Source : https://leetcode.com/problems/trapping-rain-water/
// Author : codeyu
// Date : Tuesday, October 25, 2016 11:16:36 PM
/**********************************************************************************
*
*
* Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
*
*
*
* For example,
* Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.
*
*
*
*
* The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image!
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution042
{
public static int Trap(int[] height)
{
if (height == null || height.Length == 0)
{
return 0;
}
int i = 0, j = height.Length - 1;
int area = 0;
int left = height[i];
int right = height[j];
int min = 0;
while (i <= j)
{
min = Math.Min(left, right);
if (left <= right)
{
area += Math.Max(0, min - height[i]);
left = Math.Max(min, height[i]);
i++;
}
else
{
area += Math.Max(0, min - height[j]);
right = Math.Max(min, height[j]);
j--;
}
}
return area;
}
}
}