我依旧是用两重循环,暴力求解…
结果有5个测试点通不过,超出时间限制了
class Solution:
def maxArea(self, height: List[int]) -> int:
n = len(height)
s = min(height[0],height[1])
for i in range(n):
for j in range(i+1,n):
x = (j-i)*min(height[i],height[j])
if s < x:
s = x
return s
于是我要换个方法,用双指针:
class Solution:
def maxArea(self, height: List[int]) -> int:
l,r = 0,len(height)-1
res = min(height[0],height[1])
while(l<r):
temp = (r-l)*min(height[l],height[r])
res = max(res,temp)
if height[l] <= height[r]:
l += 1
elif height[l] > height[r]:
r -= 1
return res
通过了,但效率不高
值得注意的是,我总是在加减、特殊情况这些点上犯错误!!不够细心,要找半天才找得出来错误…