#11Container With Most Water——Top 100 Liked Questions

博客围绕求解盛最多水的容器问题展开,给定一组非负整数代表竖线高度,要找出两条线与x轴构成的容器盛水量最大。作者先给出基本思想用双层嵌套循环求解,运行超时;后简化代码仍超时;最后参考双指针解法,只扫描一遍数据,成功求解,给出了运行时间和内存使用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

"""

第一次:运行时间超出限制,但感觉没错。

基本思想:双层嵌套循环,外层循环依次遍历列表中的每个元素item1,找出除该元素外,大于等于该元素的其它元素item2,并用tmp临时列表保存两者间的索引差值的绝对值,item1与最大索引差值绝对值之积为item1对应的最大容器值,保存在result中,最后返回result中最大值即为多求。注意,当item1是列表中最大元素,且没有与它相等的item2时,此时tmp为空,使用max函数会报错,在这种情况下,可忽略。

"""

class Solution(object):

    def maxArea(self, height):

        """

        :type height: List[int]

        :rtype: int

        """

        result = []

        for index1, item1 in enumerate(height):

            tmp = []

            for index2, item2 in enumerate(height):

                if index1 != index2 and item2 >= item1: 

                    tmp.append(abs(index1-index2))

            if tmp: 

                result.append(max(tmp) * item1)

            else: 

                continue

        return max(result) 

"""

Time Limit Exceeded

"""

"""

第二次:参考网上的暴力解法,发现自己第一次写的代码太啰嗦。简化如下,但是运行仍然超出时间限制

"""

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        maxarea, area = 0, 0
        for i in range(len(height)):
            for j in range(i + 1, len(height)):
                maxarea = max(maxarea, min(height[i], height[j]) * (j - i))
        return maxarea

"""

Time Limit Exceeded

"""

"""

第三次:参考网上的双指针解法:i指向开始位置,j指向结束位置,每次比较i和j位置的值大小,计算当前面积,同时将值较小的向较大的方向前移一位,更新最大面积。

为什么这么做呢?这样可以只对数据扫描一遍;面积为较短边与宽度(两边的索引之差)的乘积,高度固定情况下,宽度越大,面积越大,如果每次移动的是值较大的一边,那么计算出的面积还是较短边与宽度的乘积,且相比之前的面积更小了。但是如果移动的是值较小的一边,虽然宽度减小了,但可能高度会变大,面积不一定减小。

""" 

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        i, j = 0, len(height) - 1
        area, maxarea = 0, 0
        while i < j:
            if height[i] < height[j]:
                area = height[i] * (j - i)
                i += 1
            else:
                area = height[j] * (j - i)
                j -= 1
            maxarea = max(area, maxarea)
        return maxarea

"""

Runtime: 108 ms, faster than 43.41% of Python online submissions forContainer With Most Water.

Memory Usage: 13.2 MB, less than 5.21% of Python online submissions forContainer With Most Water.

"""

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值