题解 | #盛水最多的容器#

盛水最多的容器

https://www.nowcoder.com/practice/3d8d6a8e516e4633a2244d2934e5aa47

暴力解法  类似冒泡排序算出来,但是会超时
class Solution:
    def maxArea(self , height: List[int]) -> int:
        ##write code here
        temp=[]
        temp_max=[]
        num=len(height)
        if num<2:
            return 0
        for i in range(num):
            for j in range(i+1,num):
                if height[i]>=height[j]:
                   temp.append(height[j]*(j-i))
                else:
                    temp.append(height[i]*(j-i))
            if temp==[]:
                break
            a=max(temp)
            temp_max.append(a)
            temp.clear()
        a=max(temp_max)
        return a
双指针解法 算是一个公式 类似比大小 左右指针互相移动 假设一开始边界最大
class Solution:
    def maxArea(self , height: List[int]) -> int:
        ##write code here
        num = len(height)
        if num<2:
            return 0
        temp=0
        left=0
        right=num-1
        while left<right:
            temp_max=min(height[left],height[right]) *(right-left)
            temp=max(temp,temp_max)
            if height[left]<height[right]:
               left=left+1
            else:
               right=right-1
        return temp

全部评论

相关推荐

点赞 评论 收藏
分享
Cherrycola01:0实习 0项目 约等于啥也没有啊 哥们儿这简历认真的吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务