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

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

42. Trapping Rain Water 类似,两条垂直的线和X轴组成一个容器,灌水多少不仅与两个柱子的高度有关,也与两个柱子的距离有关,公式:S(i,j) = min(ai, aj) * (j-i),容器不能倾斜,求容纳最多水的两个线组合。

用暴力搜索Brute Force, Time: O(n2)会超时。

使用双指针two pointers。 定义left,right两个指针,两指针相遇循环结束。何时移动左右指针呢,保留较高的柱子,移动较矮的柱子。

Time complexity: O(n),  Space complexity: O(1)

Java:

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

Python:

class Solution:
    def maxArea(self, height):
        max_area, i, j = 0, 0, len(height) - 1
        while i < j:
            max_area = max(max_area, min(height[i], height[j]) * (j - i))
            if height[i] < height[j]:
                i += 1
            else:
                j -= 1
        return max_area  

C++:

class Solution {
public:
    int maxArea(const vector<int>& height) {
        int ans = 0;
        int l = 0;
        int r = height.size() - 1;
        while (l < r) {
            int h = min(height[l], height[r]);
            ans = max(ans, h * (r - l));
            if (height[l] < height[r])
                ++l;
            else
                --r;
        }
        return ans;
    }
};  

类似题目:

[LeetCode] 42. Trapping Rain Water 收集雨水

原文地址:https://www.cnblogs.com/lightwindy/p/8495693.html

时间: 2024-11-07 01:10:57

[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 contain

[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 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 cont

[LeetCode] 11. Container With Most Water My Submissions Question 解题思路

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 (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

LeetCode 11 Container With Most Water(最大水容器)

翻译 给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai). n个垂直线段例如线段的两个端点在(i,ai)和(i,0). 找到两个线段,与x轴形成一个容器,使其包含最多的水. 备注:你不必倾倒容器. 翻译 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such tha