[leetcode]Container With Most Water @ Python

原題地址:https://oj.leetcode.com/problems/container-with-most-water/spa

題意:code

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.orm

Note: You may not slant the container.blog

解題思路:兩個隔板的矮的那一個的高度乘以兩個隔板的間距就是儲水量。leetcode

代碼:get

class Solution:
    # @return an integer
    def maxArea(self, height):
        left = 0; right = len(height)-1
        res = 0
        while left < right:
            water = min(height[left], height[right]) * (right-left)
            if water > res: res = water
            if height[left] < height[right]: 
                left += 1
            else:
                right -= 1
        return res
相關文章
相關標籤/搜索