11. 盛最多水的容器(pyhton)

【题目】

示例:
输入: [1,8,6,2,5,4,8,3,7]
输出: 49

【思路】

  1. 先将list排序
  2. 然后从最小y值元素开始,此时的元素线找到离它最远的线得到x,将y*x添加到ans
  3. 遍历完所有元素,返回max(ans)
class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        h = list(sorted(height))
        length = len(h)
        n = [j for j in range(length)]
        ans = []
        #print(h,length)
        for i in range(length-1):
            if i>0 and h[i] == h[i-1]:
                height[x] = []
            x = height.index(h[i])
            xl = x - n[0]
            xr = n[-1] - x
            #print(x,xl,xr)
            if xl >= xr:
                ans.append(xl*h[i])
            else:
                ans.append(xr*h[i])
            n.remove(x)
            #print(height, n, ans)
        return max(ans)

height = [1,6,6,2,5,4,8,3,7]
#height = [3,1,3,4]
print(Solution().maxArea(height))

在这里插入图片描述

【总结】
嘿嘿,中等难度的题目完全靠自己做出来了,但是呃…代码耗时和内存消耗数据惨不忍睹,等之后学习一下别人的做法,改进一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值