11. Container With Most Water

https://leetcode.com/problems/container-with-most-water/

11. Container With Most Water

思路

https://leetcode.com/problems/container-with-most-water/discuss/6100/Simple-and-clear-proofexplanation

1. 以头尾两个柱子(指针)为baseline,‘贪婪地’移动两个指针,使其收缩

2. 指针移动的依据是改变短柱子对应的指针,向另一根柱子的方向移动

3. 如果移动的是长柱子对应的指针,新的体积比原来的体积还要小,故不考虑

AC代码

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