-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4466bbb
commit 35fb957
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/LeetCode.Solutions/NeetCode150/ArraysAndHashing/TrappingRainWaterSolution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace LeetCode.Solutions.NeetCode150.ArraysAndHashing; | ||
/// <summary> | ||
// You are given an array non-negative integers heights which represent an elevation map. Each value heights[i] represents the height of a bar, which has a width of 1. | ||
|
||
// Return the maximum area of water that can be trapped between the bars. | ||
/// </summary> | ||
[Level(Level.Hard)] | ||
public class TrappingRainWaterSolution | ||
{ | ||
[TimeComplexity("O(n)")] | ||
[SpaceComplexity("O(1)")] | ||
public int Trap(int[] height) | ||
{ | ||
|
||
if (height == null || height.Length == 0) | ||
{ | ||
return 0; | ||
} | ||
|
||
int left = 0; | ||
int right = height.Length - 1; | ||
|
||
int leftMax = height[left]; | ||
int rightMax = height[right]; | ||
int result = 0; | ||
|
||
while (left < right) | ||
{ | ||
if (leftMax < rightMax) | ||
{ | ||
left++; | ||
leftMax = Math.Max(leftMax, height[left]); | ||
result += leftMax - height[left]; | ||
} | ||
else | ||
{ | ||
right--; | ||
rightMax = Math.Max(rightMax, height[right]); | ||
result += rightMax - height[right]; | ||
} | ||
} | ||
return result; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/LeetCode.Tests/NeetCode150/TwoPointers/TrappingRainWaterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using LeetCode.Solutions.NeetCode150.ArraysAndHashing; | ||
|
||
namespace LeetCode.Tests.NeetCode150.TwoPointers; | ||
|
||
public class TrappingRainWaterTests | ||
{ | ||
[Test] | ||
public void Case1() | ||
{ | ||
var solution = new TrappingRainWaterSolution(); | ||
var result = solution.Trap([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]); | ||
|
||
result.Should().Be(6); | ||
} | ||
|
||
[Test] | ||
public void Case2() | ||
{ | ||
var solution = new TrappingRainWaterSolution(); | ||
var result = solution.Trap([4, 2, 0, 3, 2, 5]); | ||
|
||
result.Should().Be(9); | ||
} | ||
|
||
[Test] | ||
public void Case3() | ||
{ | ||
var solution = new TrappingRainWaterSolution(); | ||
var result = solution.Trap([1, 2, 3, 4, 5]); | ||
|
||
result.Should().Be(0); | ||
} | ||
|
||
[Test] | ||
public void Case4() | ||
{ | ||
var solution = new TrappingRainWaterSolution(); | ||
var result = solution.Trap([5, 4, 3, 2, 1]); | ||
|
||
result.Should().Be(0); | ||
} | ||
} |