Programing/Algorithm
[LeetCode] 11. Container With Most Water
Napoliano
2025. 2. 8. 16:47
728x90
https://leetcode.com/problems/container-with-most-water/description/
public class Solution
{
public int MaxArea(int[] height)
{
int maxArea = 0;
int maxLeftHeight = 0;
for (int i = 0; i < height.Length - 1; i++)
{
if (maxLeftHeight > height[i])
continue;
maxLeftHeight = height[i];
int maxRightHeight = 0;
for (int j = height.Length - 1; j > i; j--)
{
if (maxRightHeight > height[j])
continue;
maxRightHeight = height[j];
maxArea = Math.Max(maxArea, (j - i) * (Math.Min(height[i], height[j])));
}
}
return maxArea;
}
}
public class Solution
{
public int MaxArea(int[] height)
{
int left = 0;
int right = height.Length - 1;
int maxArea = 0;
while (left < right)
{
int area = (right - left) * (Math.Min(height[left], height[right]));
maxArea = Math.Max(maxArea, area);
if (height[left] < height[right])
++left;
else
--right;
}
return maxArea;
}
}
728x90