目录
暴力解法
时间复杂度:O(N^2)
空间复杂度:O(1)
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
max_volumn = 0
for i1, v1 in enumerate(height):
for i2, v2 in enumerate(height):
if i2>i1:
cur_volumn = (i2-i1)*min(v1,v2)
if max_volumn<cur_volumn:
max_volumn = cur_volumn
return max_volumn
温柔解法
时间复杂度:O(N)
空间复杂度:O(1)
def maxArea2(self, height):
"""
:type height: List[int]
:rtype: int
"""
n = len(height)
marea = 0
l = 0
r = n - 1
while l < r:
marea = max(marea, min(height[l], height[r]) * (r - l))
if height[l] < height[r]:
l += 1
else:
r -= 1
return marea