题解 | #盛水最多的容器#

盛水最多的容器

http://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47

贪心算法:设置左右两个指针,选取高度小的那个为上限,计算当前左右柱子围起来的水量和max哪个大即可。

public class Solution {
    public int maxArea (int[] height) {
         int left =0,right = height.length-1;
        int max = 0;
        while(left<right){
            if(height[left]<height[right]){
                max = Math.max(max,(height[left]*(right-left)));
                left++;
            }
            else {
                max = Math.max(max,(height[right]*(right-left)));
                right--;
            }
        }
        return max;
    }
}
全部评论
深层次的理论依据是什么呢?为什么要以最短的那个作为上限呢?
点赞 回复 分享
发布于 2022-12-05 13:51 广东

相关推荐

牛客40297450...:不是研究生强,是你强
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务