maxArea.java
· 656 B · Java
Raw
// https://leetcode.com/problems/container-with-most-water
class Solution {
public int maxArea(int[] height) {
var result = 0;
var left = 0;
var right = height.length - 1;
while (left < right) {
var area = (right - left) * Math.min(height[left], height[right]);
if (area > result) {
result = area;
}
if (height[left] < height[right]) {
left += 1;
} else if (height[right] < height[left]) {
right -= 1;
} else {
right -= 1;
}
}
return result;
}
}
| 1 | // https://leetcode.com/problems/container-with-most-water |
| 2 | |
| 3 | class Solution { |
| 4 | |
| 5 | public int maxArea(int[] height) { |
| 6 | var result = 0; |
| 7 | |
| 8 | var left = 0; |
| 9 | var right = height.length - 1; |
| 10 | |
| 11 | while (left < right) { |
| 12 | var area = (right - left) * Math.min(height[left], height[right]); |
| 13 | |
| 14 | if (area > result) { |
| 15 | result = area; |
| 16 | } |
| 17 | |
| 18 | if (height[left] < height[right]) { |
| 19 | left += 1; |
| 20 | } else if (height[right] < height[left]) { |
| 21 | right -= 1; |
| 22 | } else { |
| 23 | right -= 1; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return result; |
| 28 | } |
| 29 | } |