leetcode problem 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 container, such that the container contains the most water.

Note: You may not slant the container.

思路: 设Hight[i]为第i个line的高度,设立两个指针left和right初始分别指向最左端和最右端,maxArea由此得到初始面积。然后左指针不断右移,右指针不断左移直到他们相交并由此更新最大面积。移动规则如下:

             if (Height[left] < Height[right])

                  left++

             else

                  right--

      即如果左指针小,那么左指针右移。如果右指针小,那么右指针左移。

先贴出代码,再证明为什么这种移动法能够得到最大面积。

class Solution {
public:
    int maxArea(vector<int> &height) {
        int left = 0, right = height.size()-1;
        int  max_area= 0;
        while (left < right) {
            int tmp = area(min(height[left], height[right]), right - left);
            if (max_area < tmp)
                max_area = tmp;
            if (height[left] < height[right])
                ++left;
            else
                --right;
        }
        return max_area;
    }
private:
    int area(int x, int y){
        return x*y;
    }
};

下面证明这种方法可以得到最大面积:

  首先我们需要知道,left和right指针一定会遍历所有端点,且它们不会有公共的遍历点。

  然后我们可以先假设已经知道了最大面积,并设其左端点为leftMax,右端点为rightMax,且不妨设Height[leftMax] < Height[rightMax]。我们需要证明的是存在某一个时刻使得left=leftMax and right=rightMax

  

  由于两个端点构成了最大面积,那么可以得到

    (1) Height[left] < Height[leftMax]     (1 <= left < leftMax)  且

    (2 ) Height[right] < Height[leftMax]  (leftMax+1 <=right <=totalNum)

  根据假设:

    (3) Height[leftMax] < Height[rightMax]

那么根据算法的规则:如果左端点比右端点小,左端点右移,反之亦然。那么由此可得:

    (4)如果left先到达leftMax,而right还未到达rightMax,那么根据(2),right一定会一直移动到rightMax而left呆在leftMax不动

    (5)如果right先到达rightMax,而left还未达到leftMax,那么根据(1)和(3),left一定会一直移动到leftMax而right呆在rightMax不懂。

  根据(4)和(5)得到一定存在某一个时刻使得left=leftMax and right=rightMax。

时间: 2024-08-28 10:36:48

leetcode problem 11 Container With Most Water的相关文章

【LeetCode】11. Container With Most Water 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937 Subject 出处:https://leetcode.com/problems/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

【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 Array 11 Container With Most Water

public class Solution { public int maxArea(int[] height) { int maxa = 0; int maxi = 0; if(height.length<2) return 0; for(int i=0;i<height.length-1;i++){ if(height[i]<height[maxi]){ continue; } for(int j=height.length-1;j>i;j--){ if(height[j]&g

11. Container With Most Water【leetcode】,java算法,数组,求最大水池注水量

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 togethe

LeetCode.ZigZag Conversion ,Container With Most Water

ZigZag Conversion: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by lin

刷题11. Container With Most Water

一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<iostream> #include<vector> #include<math.h> using namespace std; class Solution{ public: int maxArea(vector<int> &height){ int max =

[leecode]---11.container with most water

description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数,将其与次大,第三大数求面积,依次类推,也需要两层循环,还需要额外排序,时间复杂度n2 因为找出最大数并且并不知道输入数据的规律(有可能很杂乱),所以每个都有必要算,采取思路1. 代码实现如下: class Solution { public int maxArea(int[] height) {

LeetCode 11 Container With Most Water (C,C++,Java,Python)

Problem: 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

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