7 Container With Most Water_Leetcode

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.

这题O(n^2)的解法就是基本的暴力法。但是要想出O(n)的解法并不容易(实际上我就想不出来T_T),面试碰到这样难度的新题估计就跪了。

虽然有点悲观,但是解题的思路还是有迹可循的。

container的面积由两个因素组成,一个是高度,一个是宽度。

我们同样采用的是“按序枚举”的思路,高度是不确定的变量,不好枚举,但是宽度则是由1到n-1的确定的量,可以很方便的枚举。

此外,如同上题Candy,这种按序枚举的思路也可以是从左到右,从右往左等。

在想到对宽度进行枚举后,这种枚举有两种,一个是从小往大,另一个是从大往小。

如果我们想从小往大枚举,这种一般必须有dp的规律才能够使解法更优,但是从本题来看,每一个解都只与左右两个index有关,并不包含子问题。所以从小往大的枚举不可行。

那么从大往小枚举,比如3,2,5,4,1。最大的宽度,就是从3到1,这时我们可以算出一个面积。当我们缩小这个宽度,有两种方法,但是实际上只有右区间缩小一个可能得到最优解。为什么呢?因为每次对于左右边界中较小的那一个,当前的宽度都是最宽的,也就是它能达到的最大面积了。

由此我们可以只往一个方向进行左右边界的缩小,最终得到的方法是O(n)的。

Code:

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

  

posted @ 2014-10-16 21:56  Avril  阅读(401)  评论(0编辑  收藏  举报