Skip to content

Commit 49ad52e

Browse files
committed
Add a solution to the Container With Most Water (Max Area) problem.
1 parent a277030 commit 49ad52e

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Container With Most Water
2+
3+
You are given an integer array `height` of length `n`.
4+
There are `n` vertical lines drawn such that the 2 endpoints of the `i-th` line are `(i, 0)` and `(i, height[i])`.
5+
6+
Find 2 lines that together with the `x-axis` form a container, such that the container contains the most water.
7+
Return the maximum amount of water a container can store.
8+
Notice that you may not slant the container.
9+
10+
## My Note:
11+
12+
We just need to find the max area of a rectangle formed by two lines and an x-axis.
13+
14+
## Examples
15+
16+
### Example 1:
17+
18+
![Example](Example.png)
19+
20+
Input: height = [1,8,6,2,5,4,8,3,7]
21+
Output: 49
22+
Explanation:
23+
The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7].
24+
In this case, the max area of water (blue section, see the picture) the container can contain is 49.
25+
26+
## Constraints:
27+
28+
- `n == height.length`
29+
- `2 <= n <= 105`
30+
- `0 <= height[i] <= 104`
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace LeetCode.Challenges.ContainerWithMostWater;
2+
3+
// Time Complexity:
4+
// The loop continues until left is no longer less than right.
5+
// Since left starts from `0` and right starts from `heights.Length - 1`,
6+
// and one of them is moved closer to the other in each iteration,
7+
// the loop will execute at most `n - 1` times where `n` is the length of the `heights` array.
8+
// The time complexity of the algorithm is O(n).
9+
//
10+
// Space Complexity of the algorithm is O(1).
11+
public static class Solution
12+
{
13+
public static int GetMaxArea(int[] heights)
14+
{
15+
var leftPointer = 0;
16+
var rightPointer = heights.Length - 1;
17+
var maxArea = int.MinValue;
18+
while (leftPointer < rightPointer)
19+
{
20+
var leftEdgeHeight = heights[leftPointer];
21+
var rightEdgeHeight = heights[rightPointer];
22+
var minEdgeHeight = Math.Min(leftEdgeHeight, rightEdgeHeight);
23+
var currentArea = minEdgeHeight * (rightPointer - leftPointer);
24+
if (maxArea < currentArea)
25+
{
26+
maxArea = currentArea;
27+
}
28+
29+
if (leftEdgeHeight < rightEdgeHeight)
30+
{
31+
leftPointer++;
32+
}
33+
else
34+
{
35+
rightPointer--;
36+
}
37+
}
38+
39+
return maxArea;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using LeetCode.Challenges.ContainerWithMostWater;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace LeetCode.Challenges.UnitTests.ContainerWithMostWater;
6+
7+
public class SolutionTests
8+
{
9+
[Theory]
10+
[MemberData(nameof(TestData))]
11+
public void GivenHeights_WhenCallGetMaxArea_ThenResultAsExpected(int[] heights, int expectedResult)
12+
{
13+
var actualResult = Solution.GetMaxArea(heights);
14+
actualResult.ShouldBeEquivalentTo(expectedResult);
15+
}
16+
17+
public static IEnumerable<object[]> TestData()
18+
{
19+
yield return [new[] { 1, 8, 6, 2, 5, 4, 8, 3, 7 }, 49];
20+
yield return [new[] { 1, 1 }, 1];
21+
}
22+
}

0 commit comments

Comments
 (0)