5.盛最多水的容器
小于 1 分钟
5.盛最多水的容器
参考链接
优秀题解
class Solution {
public int maxArea(int[] height) {
int i = 0, j = height.length - 1, res = 0;
while(i < j) {
// 个人对题解的理解:
// 短板向内移动,宽度-1,长度可能变短 / 变长,求面积时长度可能增长到与长板的长度相同;
// 长板向内移动,宽度-1,长度可能变短 / 变长,但是也无法改变,求面积时长度只能到短板的长度
res = height[i] < height[j] ?
Math.max(res, (j - i) * height[i++]):
Math.max(res, (j - i) * height[j--]);
}
return res;
}
}
作者:Krahets
链接:https://leetcode.cn/problems/container-with-most-water/solutions/11491/container-with-most-water-shuang-zhi-zhen-fa-yi-do/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。