Apparently volume = min(left height, right height) * distance. Because we don't know the arrange of heights, we just start from start and end with largest distance, and try to keep as large height as possible.
Solution:
# T:O(n) S:O(1)
class Solution:
# @return an integer
def maxArea(self, height):
max_area, i, j = 0, 0, len(height) - 1
while i < j:
max_area = max(max_area, min(height[i], height[j]) * (j - i))
if height[i] < height[j]:
i += 1
else:
j -= 1
return max_area
Run Time: 64 ms
No comments:
Post a Comment