description:

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:

Example:

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

my answer:

感恩

设置左右两个指针,每步都是随时更新结果,之后计算完结果,把短的那条边往里边挪一步。

很诡异的想法,没有理论证明下一个。谁能用数学式子证明麻烦大佬评论里点播一下
捕捉一只大佬:https://blog.csdn.net/qq_36721548/article/details/80159570
注意到如果从取值的两端向中间遍历,若是两条垂直线中的一条短于另一条,则之后保留该条垂直线的所有容器,容量都不可能超过该容器,因为随着遍历向中间进行,容器的长度在缩小,而这样遍历得到的容器的高度不可能超过初始高度,从而体积也更小。因此之后的遍历若想得到更大的容器,则一定不能保留较短的那条垂直线。这导出了如下算法:遍历由列表的两端向中间进行,每次较短的那条垂直线向中间移动一个单位,由对应的另一条垂直线代替;每次计算对应容器的容积,保留这些容积中的最大值;当两条垂直线靠在一起时,遍历结束,此时已经得到了可能的最大容积。这种算法只需进行一层循环,时间复杂度为O(n),因而更好。

大佬的answer:

class Solution {
public:
    int maxArea(vector<int>& height) {
        int res = 0, i = 0, j = height.size() - 1;
        while(i < j){
            res = max(res, min(height[i], height[j])*(j -i));
            height[i] < height[j] ? i++ : --j;
        }
        return res;
    }
};

relative point get√:

hint :