-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrappingRainWater.cs
More file actions
106 lines (92 loc) · 3.16 KB
/
TrappingRainWater.cs
File metadata and controls
106 lines (92 loc) · 3.16 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
namespace Solutions.Problems;
public class TrappingRainWaterSolution
{
/// <summary>
/// Brute Force Solution
/// </summary>
/// <param name="height">The array representing the elevation map.</param>
/// <returns>The total amount of trapped rainwater.</returns>
public int TrapBruteForce(int[] height)
{
int trappedWater = 0;
for (int i = 1; i < height.Length - 1; i++)
{
int leftMax = height[..i].Max();
int rightMax = height[(i + 1)..].Max();
trappedWater += Math.Max(0, Math.Min(leftMax, rightMax) - height[i]);
}
return trappedWater;
}
/// <summary>
/// Dynamic Programming Solution
/// </summary>
/// <param name="height">The array representing the elevation map.</param>
/// <returns>The total amount of trapped rainwater.</returns>
public int TrapDynamicProgramming(int[] height)
{
int n = height.Length;
if (n == 0) return 0;
int[] leftMax = new int[n];
int[] rightMax = new int[n];
leftMax[0] = height[0];
for (int i = 1; i < n; i++)
leftMax[i] = Math.Max(leftMax[i - 1], height[i]);
rightMax[^1] = height[^1];
for (int i = n - 2; i >= 0; i--)
rightMax[i] = Math.Max(rightMax[i + 1], height[i]);
int trappedWater = 0;
for (int i = 0; i < n; i++)
trappedWater += Math.Min(leftMax[i], rightMax[i]) - height[i];
return trappedWater;
}
/// <summary>
/// Stack Solution
/// </summary>
/// <param name="height">The array representing the elevation map.</param>
/// <returns>The total amount of trapped rainwater.</returns>
public int TrapStack(int[] height)
{
int trappedWater = 0;
Stack<int> stack = new();
for (int i = 0; i < height.Length; i++)
{
while (stack.Count > 0 && height[i] > height[stack.Peek()])
{
int top = stack.Pop();
if (stack.Count == 0) break;
int distance = i - stack.Peek() - 1;
int boundedHeight = Math.Min(height[i], height[stack.Peek()]) - height[top];
trappedWater += distance * boundedHeight;
}
stack.Push(i);
}
return trappedWater;
}
/// <summary>
/// Two Pointers Solution
/// </summary>
/// <param name="height">The array representing the elevation map.</param>
/// <returns>The total amount of trapped rainwater.</returns>
public int Trap(int[] height)
{
(int left, int right) = (0, height.Length - 1);
(int leftMax, int rightMax) = (height[left], height[right]);
int trappedWater = 0;
while (left < right)
{
if (leftMax < rightMax)
{
left++;
leftMax = Math.Max(leftMax, height[left]);
trappedWater += leftMax - height[left];
}
else
{
right--;
rightMax = Math.Max(rightMax, height[right]);
trappedWater += rightMax - height[right];
}
}
return trappedWater;
}
}