leetcode 11题 水池最大容积

题目描述

给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

思路:

这是一道动态规划的问题,有两个思路,第一个思路是从左到右 寻找最大的水槽面积,其实就是新的标杆和最左标杆,最右标杆,原始面积三个面积的最大值,然后新标杆不断增长,最终得到最大的面积

第二个思路便是不断变小,也就是最左标杆和最右标杆,然后二者不断缩小最小值。

第一种思路:

    def maxArea(height):
left = 0
right = 1
count = 1
distance = right - left
maxwater = min(height[left], height[right]) * distance
while (count < len(height)-1):
count += 1
nextright = count
print(count)
nextleftwater = min(height[left], height[nextright]) * (nextright - left)
nextrightwater = min(height[right], height[nextright]) * (nextright - right)
templist = [maxwater, nextleftwater, nextrightwater]
if templist.index(max(templist)) == 0:
continue
if templist.index(max(templist)) == 1:
right = nextright
maxwater = nextleftwater
else:
left = right
right = nextright
maxwater = nextrightwater
return maxwater

问题:

这种并不能通过所有的测试案例,问题就是在于如果之前增长是相同的,如果单纯不变,那么对于下一个面积的计算就会有问题,比如[1,2,4,3]就会出现问题,根本原因还是因为没有覆盖所有的转态空间,导致异常,但是想修改起来就非常麻烦。

第二种思路

使用双指针解法,也就是标准解法,使用不断缩小两条边,我们可以发现其实如果不断缩小的话,面积不会出现第一种思路的问题了。

class Solution:
def maxArea(self, height: List[int]) -> int:
left = 0
right = len(height)-1
maxwater = min(height[left],height[right])*(right-left)
while(left < right):
if height[left] < height[right]:
left += 1
tempwater = min(height[left],height[right])*(right-left)
if tempwater > maxwater:
maxwater = tempwater
else:
right -= 1
tempwater = min(height[left],height[right])*(right-left)
if tempwater > maxwater:
maxwater = tempwater
return maxwater

再解释一下为什么这样是对的。

首先,如果我们将左标杆加一,那么实际情况中面积很肯能会变小,应为距离缩短了,除非出现缩短了左边以后左标杆变长了,总体面积才会增加.

借用题解中一个人的非常好的思路(雫空)这个人的思路,非常感谢他的分享,讲的很透彻,

https://leetcode-cn.com/problems/container-with-most-water/solution/zhi-guan-de-shuang-zhi-zhen-fa-jie-shi-by-na-kong/

用h(i)表示第i条线段的高度,S(ij)表示第i条线段和第j条线段圈起来的面积。

可知 h(0) < h(7),从而S(07) = h(0) * 7。

有S(06) = min(h(0), h(6)) * 6。
当h(0) <= h(6),有S(06) = h(0) * 6;
当h(0) > h(6),有S(06) = h(6) * 6,S(06) < h(0) * 6。

由此可知,S(06)必然小于S(07)。

leetcode 最大水池的更多相关文章

  1. LeetCode 11 水池蓄水问题

    今天给大家分享的是一道LeetCode中等难度的题,难度不大,但是解法蛮有意思.我们一起来看题目: Link Container With Most Water Difficulty Medium 题 ...

  2. [LeetCode] Swim in Rising Water 在上升的水中游泳

    On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j). Now rain star ...

  3. 【LeetCode】778. Swim in Rising Water 水位上升的泳池中游泳(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/swim-in- ...

  4. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  6. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  7. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  8. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  9. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. H3C端口状态

  2. H3C调试信息输出的例子

  3. 2018-5-19-创建不带BOM-的UTF8

    title author date CreateTime categories 创建不带BOM 的UTF8 lindexi 2018-05-19 14:11:33 +0800 2018-2-13 17 ...

  4. 2019-8-31-C#-如何写-DEBUG-输出

    title author date CreateTime categories C# 如何写 DEBUG 输出 lindexi 2019-08-31 16:55:58 +0800 2018-2-13 ...

  5. CF1169(div2)题解报告

    CF1169(div2)题解报告 A 不管 B 首先可以证明,如果存在解 其中必定有一个数的出现次数大于等于\(\frac{m}{2}\) 暴力枚举所有出现次数大于等于$\frac{m}{2} $的数 ...

  6. <% %>、<%! %>和<%= %>

    <%  %> 此标签内的内容在jsp编译的时候,将被编译成servlet的_jspService()方法.这个方法用作服务器端向客户端输出.因此这对标签里边不能在定义方法了,因为在Java ...

  7. 【2016常州一中夏令营Day3】

    小 W 摆石子[问题描述]小 W 得到了一堆石子,要放在 N 条水平线与 M 条竖直线构成的网格的交点上.因为小 M 最喜欢矩形了,小 W 希望知道用 K 个石子最多能找到多少四边平行于坐标轴的长方形 ...

  8. SAPI(PHP常见的四种运行模式)

    SAPI(Server Application Programming Interface)服务器应用程序编程接口,即PHP与其他应用交互的接口,PHP脚本要执行有很多方式,通过Web服务器,或者直接 ...

  9. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  10. qt添加cef库嵌入web,linux 下Qt WebEngine 程序打包简单记录

    http://www.cnblogs.com/oloroso/p/6051631.html http://www.cnblogs.com/oloroso/p/6149000.html