-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrappingRainWater.cs
78 lines (60 loc) · 2.21 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
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LeetCodeSolutions
{
// Source: https://leetcode.com/problems/trapping-rain-water/
// Author: Jalal Shabo
// Date : 11/19/2023
// Time Complexity: O(n) --- for both solutions, only look through array n times
// Space Complexity: O(n) for array solution, and O(1) for two pointer solution
public static class TrappingRainWater
{
public static int TrappingRainWatereSolution(int[] height){
if(height.Length < 2){
return 0;
}
int maxL = height[0], maxR = height[height.Length-1];
int maxWaterContained = 0;
for (int l = 0, r = height.Length-1; l < r;)
{
if(maxL < maxR){
l++;
maxL = Math.Max(height[l],maxL);
maxWaterContained += maxL - height[l];
}else{
r--;
maxR = Math.Max(height[r],maxR);
maxWaterContained += maxR - height[r];
}
}
return maxWaterContained;
}
public static int TrappingRainWaterNSpaceSolution(int[] height){
int[] maxLeft = new int[height.Length];
int[] maxRight = new int[height.Length];
int[] minLeftRight = new int[height.Length];
for (int i = 0, currentMax = 0; i < height.Length-1; i++)
{
maxLeft[i] = currentMax;
if(currentMax < height[i]){
currentMax = height[i];
}
}
for (int i = height.Length-1, currentMax = 0; i > 0; i--)
{
maxRight[i] = currentMax;
if(currentMax < height[i]){
currentMax = height[i];
}
}
for (int i = 0; i < minLeftRight.Length-1; i++)
{
int currentValue = Math.Min(maxLeft[i],maxRight[i]) - height[i];
minLeftRight[i] = currentValue >= 0 ? currentValue : 0;
}
return minLeftRight.Sum();
}
}
}