[LeetCode]11. Container With Most Water 盛最多水的容器

Given n non-negative integers a1, a2, ..., a, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

题目给出[a1,a2...ai],要求找出一个ai,aj是的min(ai,aj)*(j-i)最大。有两种方法:(1)暴力法两层循环从左到右遍历,算出每一组的面积大小
public class Solution {
    public int maxArea(int[] height) {
        int maxarea = 0;
        for (int i = 0; i < height.length; i++)
            for (int j = i + 1; j < height.length; j++)
                maxarea = Math.max(maxarea, Math.min(height[i], height[j]) * (j - i));
        return maxarea;
    }
}

两层循环,复杂度自然是O(n^2)

(2)双指针法

现在假设[a1,a2......an],现在我们算出s=min(a1,an)*(n-1),1和n是长度的边界,剩下的任意高度组合的长度都不能比n-1大,长度如果变成了n-2,

之前算出的面积s如果不想变小,min(ai,aj)就必须比min(a1,an)大。总结一下就是,随着长度的缩小,面积要想变大就必须扩大高度,所以比原先高度低的我们就

不需要再考虑了。

public class Solution {
    public int maxArea(int[] height) {
        int maxarea = 0, l = 0, r = height.length - 1;
        while (l < r) {
            maxarea = Math.max(maxarea, Math.min(height[l], height[r]) * (r - l));
            if (height[l] < height[r])
                l++;
            else
                r--;
        }
        return maxarea;
    }
}

一次遍历就能解决,时间复杂度O(n)

原文地址:https://www.cnblogs.com/jchen104/p/10201901.html

时间: 2024-11-05 21:37:31

[LeetCode]11. Container With Most Water 盛最多水的容器的相关文章

[LeetCode] 11. Container With Most Water 装最多水的容器

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

[LeetCode] Container With Most Water 装最多水的容器

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

[LintCode] Container With Most Water 装最多水的容器

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

[leetcode] 11.盛最多水的容器

盛最多水的容器 题意好绕,看半天都没懂要干什么. 我直接上个图可能就能一眼看明白了: 总之,当明白题意后,直接就能想到暴力法枚举所有可能性,不过如果再画一下图基本就能发现:两线段之间形成的区域总是会受到其中较短那条长度的限制. 我举个例子,对于所有以(1,a1)为左边构成的矩阵,最大的一个肯定在右边(n,an)开始数,第一个大于等于ai的边,我们记成aj.即以a1为左边的最大矩阵肯定是 以right边从(j,aj)~(n,an)构成矩阵中的其中一个.实际上,对于(1,a1)~(j,aj)的边,我

11. 盛最多水的容器

11. 盛最多水的容器 方法一 class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ h_len = len(height) i = 0 k = -1 s = [] while h_len + k - i >= 1: s.append(min(height[i], height[k]) * (h_len +

Leetcode_11【盛最多水的容器】

文章目录: 题目 脚本一及注释 脚本一逻辑 脚本二及注释 脚本二逻辑 题目: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水. 说明:你不能倾斜容器,且 n 的值至少为 2. 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7].在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 4

Leetcode 11. Container With Most Water (two pointers)

Leetcode: 11 there are two ways to deal with two pointers one is O(n), two pointers moves from both side Another is O(2N), two pointer move from the same side Idea for this, choose the first one and then if there is a smaller one, change that corresp

LeetCode 11. Container With Most Water (装最多水的容器)

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

[leetcode]11. Container With Most Water存水最多的容器

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a contain