[leetcode]11. Container With Most Water[UNSOLVED]

时间:2022-05-20 21:55:51

随手写一个循环,42testcase TLE。

从两侧向*循环会漏条件,先放着吧。

class Solution:
    def maxArea(self, height: List[int]) -> int:
        #non-negative mean first xiangxian
        #n > 2
        lengthH = len(height)
        if lengthH == 2:
            if height[0] >= height[1]:
                return height[1]
            else:
                return height[0]
        else:
            #normal
            area = []
            for start in range(lengthH -1):
                for end in range(start +1,lengthH):
                    if height[start] >= height[end]:
                        area.append(height[end]*(end - start))
                    else:
                        area.append(height[start]*(end - start))
            return max(area)