classSolution(object): defmaxArea(self, height): """ :type height: List[int] :rtype: int """ # O(n^2) time limit了 """ max_water = 0 for i in range(len(height)): for j in range(i, len(height)): max_water = max(max_water, (j-i)*min(height[i], height[j])) return max_water """
while i < j: if height[i] <= height[j]: # 等号随便给一边就可以了 while i < j and height[i] <= height[j]: max_water = max(max_water, (j-i) * min(height[i], height[j])) i += 1 else: while j > i and height[j] <= height[i]: max_water = max(max_water, (j-i) * min(height[i], height[j])) j -= 1