-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path011-ContainerWithMostWater.cs
41 lines (37 loc) · 1.17 KB
/
011-ContainerWithMostWater.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
//-----------------------------------------------------------------------------
// Runtime: 104ms
// Memory Usage: 27.5 MB
// Link: https://leetcode.com/submissions/detail/378092152/
//-----------------------------------------------------------------------------
using System;
namespace LeetCode
{
public class _011_ContainerWithMostWater
{
public int MaxArea(int[] height)
{
int left = 0, right = height.Length - 1;
int result = 0;
while (left < right)
{
var area = Math.Min(height[left], height[right]) * (right - left);
result = Math.Max(result, area);
if (height[left] <= height[right])
{
var temp = height[left];
do
left++;
while (left < right && height[left] <= temp);
}
else
{
var temp = height[right];
do
right--;
while (left < right && height[right] <= temp);
}
}
return result;
}
}
}