Day7 盛最多水的容器

作者: Shimmer_ | 来源:发表于2021-02-01 07:35 被阅读0次

给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水

https://leetcode-cn.com/problems/container-with-most-water/

示例1:

image

输入:[1,8,6,2,5,4,8,3,7]
输出:49
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

示例2:

输入:height = [1,1]
输出:1

示例3:

输入:height = [4,3,2,1,4]
输出:16

示例4:

输入:height = [1,2,1]
输出:2

提示:

  • n = height.length
  • 2 <= n <= 3 * 104
  • 0 <= height[i] <= 3 * 104

思路:

  • 面积=两点差值*两点最小值;由此可以通过左侧遍历获取最大值
  • 最简单的方法,一次就过但效率不行,官方解很NICE,当时考虑过,但没找到移到条件
package sj.shimmer.algorithm;

/**
 * Created by SJ on 2021/1/31.
 */

class D7 {
    public static void main(String[] args) {
        System.out.println(maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}));
        System.out.println(maxArea(new int[]{1,1}));
        System.out.println(maxArea(new int[]{4,3,2,1,4}));
        System.out.println(maxArea(new int[]{1,2,1}));
        System.out.println(maxArea(new int[]{}));
    }

    public static int maxArea(int[] height) {
        if (height == null || height.length <= 1) {
            return 0;
        }
        int result = 0;
        for (int i = 0; i < height.length; i++) {
            for (int j = 1; j < height.length; j++) {
                int tempR = Math.min(height[i], height[j]) * (j - i);
                result = result < tempR?tempR:result;
            }
        }
        return result;
    }
}
image

官方解

  1. 双指针

    • 因为 面积=两点差值两点最小值*,从两边开始分别遍历,左右指针指向头尾,此时差值最大
    • 开始移动时即可将左右指针指向较小的点的指针向内移动(移动较大的会导致两个因数都变小,导致面积变小移动较小的会导致差值变小,但两点最小值可能变大,面积也有变大的可能
     public static int maxArea1(int[] height) {
            if (height == null || height.length <= 1) {
                return 0;
            }
            int left = 0;int right = height.length-1;
            int result = 0;
            while (left!=right){
                if (height[left]>height[right]) {
                    int tempR = height[right] * (right - left);
                    result = tempR>result?tempR:result;
                    right--;
                }else {
                    int tempR = height[left] * (right - left);
                    result = tempR>result?tempR:result;
                    left++;
                }
            }
            return result;
        }
    
    image
    • 时间复杂度:O(N),双指针总计最多遍历整个数组一次。
    • 空间复杂度:O(1),只需要额外的常数级别的空间

相关文章

  • Day7 盛最多水的容器

    给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直...

  • 盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直...

  • 盛最多水的容器

    盛最多水的容器 给定n个非负整数a1,a2,...,an,每个数代表坐标中的一个点(i,ai) 。画n条垂直线,使...

  • 盛最多水的容器

    题目来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/cont...

  • 盛最多水的容器

    题目描述 思路 题解代码

  • 盛最多水的容器

    盛最多水的容器 难度中等1826 收藏 分享 切换为英文 关注 反馈 给你 n 个非负整数 a1,a2,...,a...

  • 盛最多水的容器

    题目描述:  给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内...

  • 盛最多水的容器

    给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直...

  • 盛最多水的容器

    题目 https://leetcode-cn.com/problems/container-with-most-w...

  • 盛最多水的容器

    题目: 题目的理解: 计算两个数之间的面积,获取到最大的面积。 时间复杂度为O(n!), 当数量越大计算时间越长,...

网友评论

    本文标题:Day7 盛最多水的容器

    本文链接:https://www.haomeiwen.com/subject/eowltltx.html