滑动窗口
11:盛水最多的容器
关键:需要找到长的板和长的距离
解法一:暴力法,类似冒泡的双重循环,优化后时间复杂度为O(√n),不符合要求。
解法二:双指针,从头尾往中间凑,不断更新长板和面积,时间复杂度为O(㏒n),Python3代码如下。
class Solution:
def solveProblem(self, height:list) -> int:
left, right, area, new_area = 0, len(height)-1, 0, 0
while right > left:
lh, rh = height[left], height[right]
if lh > rh:
new_area = rh * (right - left)
right -= 1
else:
new_area = lh * (right - left)
left += 1
if new_area > area: area = new_area
return area
42:接雨水
关键:想象一个极值窗口,在区间上滑动,减去窗口内占去,
标签:right,area,lh,height,力扣,刷题,new,Kyana,left From: https://www.cnblogs.com/kyana/p/18088039