美文网首页
贪心: 盛最多水的容器

贪心: 盛最多水的容器

作者: HellyCla | 来源:发表于2023-04-24 09:11 被阅读0次
image.png

一个核心的思想是,底边与高共同决定面积,那么使用双指针,指向两端,获得最大底边。由于短板决定最大面积,因此只有移动短板才有可能获得更大面积。因此总是移动短板的指针。

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

相关文章

  • 盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直...

  • 盛最多水的容器

    盛最多水的容器 给定n个非负整数a1,a2,...,an,每个数代表坐标中的一个点(i,ai) 。画n条垂直线,使...

  • 盛最多水的容器

    题目来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/cont...

  • 盛最多水的容器

    题目描述 思路 题解代码

  • 盛最多水的容器

    盛最多水的容器 难度中等1826 收藏 分享 切换为英文 关注 反馈 给你 n 个非负整数 a1,a2,...,a...

  • 盛最多水的容器

    题目描述:  给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内...

  • 盛最多水的容器

    给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直...

  • 盛最多水的容器

    题目 https://leetcode-cn.com/problems/container-with-most-w...

  • 盛最多水的容器

    题目: 题目的理解: 计算两个数之间的面积,获取到最大的面积。 时间复杂度为O(n!), 当数量越大计算时间越长,...

  • 盛最多水的容器

    LeetCode第十一题 题目描述:给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i...

网友评论

      本文标题:贪心: 盛最多水的容器

      本文链接:https://www.haomeiwen.com/subject/agkujdtx.html