其他分享
首页 > 其他分享> > 20211108-02 Container With Most Water

20211108-02 Container With Most Water

作者:互联网

20211108-02 Container With Most Water

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

image
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

python

class Solution(object):
    def maxArea(self, height):
        left, right = 0, len(height) - 1
        res = 0         # 定义初始值为 -inf, 因为盛的水一定不能是负数,所以初始化为0

        while left < right:
            if height[left] < height[right]:
                res = max(res, height[left] * (right - left))
                left += 1
            else:
                res = max(res, height[right] * (right - left))
                right -= 1
        return res

标签:02,right,Container,res,height,Water,Most,max,left
来源: https://www.cnblogs.com/dundundun/p/15522743.html