https://leetcode.cn/problems/container-with-most-water/
双指针
思路:
容器容积 = (right - left) * min{height[left],height[right]}
- 初始时,left = 0,right = len - 1
- 此时(right - left)最大,那么移动两侧指针,right - left 单调减小
- 所以需要寻找更大的min{height[left],height[right]},此时较小一边点指针就需要移动
- 每次移动完毕后,将会得到一个新的结果,若比原结果大,update一下
- 继续移动较小一边,直到不满足left < right 这个限制条件
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int result = 0;
while(left < right) {
result = Math.max(result,(right - left) * (Math.min(height[left],height[right])));
if(height[left] < height[right]) {
left ++;
} else {
right --;
}
}
return result;
}
标签:容器,right,盛水,int,min,height,LeetCode11,result,left
From: https://www.cnblogs.com/k-young/p/16623419.html