/**
* Two pointer.
* Left and right are the two pointers indicate tallest lines,
* whenever there is a taller line update the corresponding pointer.
* Note that the volumn is determined by the shorter line.
*/
public class Solution {
public int maxArea(int[] height) {
int max = 0, left = 0, right = height.length-1;
while (left < right) {
max = Math.max(max, (right-left)*Math.min(height[left], height[right]));
// update the left pointer
if (height[left] < height[right])
while (left < right) {
if (height[left] < height[left+1]) {
left++;
break;
}
left++;
}
// update the right pointer
else {
while (left < right) {
if (height[right] < height[right-1]) {
right--;
break;
}
right--;
}
}
}
return max;
}
}
Leetcode 11. Container With Most Water
最新推荐文章于 2022-07-23 19:08:46 发布