
image.png
一個(gè)核心的思想是,底邊與高共同決定面積,那么使用雙指針,指向兩端,獲得最大底邊。由于短板決定最大面積,因此只有移動(dòng)短板才有可能獲得更大面積。因此總是移動(dòng)短板的指針。
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
lp=0
rp=len(height)-1
area=0
while lp<rp:
cur=min(height[lp],height[rp])*(rp-lp)
area=max(cur,area)
if height[lp]<height[rp]:
lp+=1
else:
rp-=1
return area

image.png