g0001_0100.s0011_container_with_most_water.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0011_container_with_most_water;
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Greedy #Two_Pointers
// #Algorithm_II_Day_4_Two_Pointers #Big_O_Time_O(n)_Space_O(1)
// #2024_02_11_Time_3_ms_(95.71%)_Space_58.1_MB_(33.16%)
public class Solution {
public int maxArea(int[] height) {
int maxArea = -1;
int left = 0;
int right = height.length - 1;
while (left < right) {
if (height[left] < height[right]) {
maxArea = Math.max(maxArea, height[left] * (right - left));
left++;
} else {
maxArea = Math.max(maxArea, height[right] * (right - left));
right--;
}
}
return maxArea;
}
}