2020-04-18(11. 盛最多水的容器**)

难度 中等
这道题看到评论和题解数就知道不会太难。第一遍直接暴力法实现,执行时间不理想。第二遍使用双指针,没有特别的难点,关键在于思路。
方法一:
执行用时 :484 ms, 在所有 Java 提交中击败了15.30%的用户
内存消耗 :40 MB, 在所有 Java 提交中击败了27.86%的用户

public int maxArea(int[] height) {
        int result = 0;
        for(int i = 0; i < height.length; i++){
            for(int j = height.length-1; j > i; j--){
                int v = (j-i) * Math.min(height[i], height[j]);
                result = Math.max(result, v);
            }
        }
        return result;
}

方法二:
执行用时 :3 ms, 在所有 Java 提交中击败了92.82%的用户
内存消耗 :39.8 MB, 在所有 Java 提交中击败了45.00%的用户

    public int maxArea(int[] height) {
        int result = 0;
        int l = 0;
        int r = height.length-1;
        while(l < r){
            int v = 0;
            if(height[l] < height[r]){
                v = (r-l)*height[l];
                l++;
            }else{
                v = (r-l)*height[r];
                r--;
            }
            result = Math.max(result, v);
        }
        return result;
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容