LeetCode : No11 Container With Most Water

来源:互联网 发布:淘宝网甩棍多少钱一根 编辑:程序博客网 时间:2024/04/30 11:28

题目链接:https://leetcode.com/problems/container-with-most-water/

 

直接看discuss里面的讨论。。。

The idea is : to compute area, we need to take min(height[i],height[j]) as our height. Thus ifheight[i]<height[j], then the expressionmin(height[i],height[j]) will always lead to at maximumheight[i] for all other j(i being fixed), hence no point checking them. Similarly whenheight[i]>height[j] then all the other i's can be ignored for that particular j.

 

耗时:91ms

 

class Solution:    # @return an integer    def maxArea(self, height):        l = 0        n = len(height) - 1                MAX = 0        while (l < n):            MAX = max(MAX, (n-l)*min(height[l],height[n]))            if (height[l] < height[n]):                temp = height[l]                l  = l + 1                while (l+1 < n and temp >= height[l]):                    l = l + 1            else:                temp = height[n]                n = n - 1                while (l < n-1 and temp >= height[n]):                    n = n - 1        return MAX


 

0 0