leetcode 最大水池
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
再解释一下为什么这样是对的。
首先,如果我们将左标杆加一,那么实际情况中面积很肯能会变小,应为距离缩短了,除非出现缩短了左边以后左标杆变长了,总体面积才会增加.
借用题解中一个人的非常好的思路(雫空)这个人的思路,非常感谢他的分享,讲的很透彻,
用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 最大水池的更多相关文章
- LeetCode 11 水池蓄水问题
今天给大家分享的是一道LeetCode中等难度的题,难度不大,但是解法蛮有意思.我们一起来看题目: Link Container With Most Water Difficulty Medium 题 ...
- [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 ...
- 【LeetCode】778. Swim in Rising Water 水位上升的泳池中游泳(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/swim-in- ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
随机推荐
- P1073 奇数还是偶数
题目描述 给你 \(N(1 \le N \le 1000)\) 个数,你需要判断每个数是奇数还是偶数. 输入格式 输入的第一行包含一个整数 \(N(1 \le N \le 1000)\) . 接下来 ...
- BAT 脚本判断当前系统是 x86 还是 x64 系统
本文告诉大家在写 BAT 脚本的时候,如何判断当前的系统是 32 位系统的还是 64 位系统 通过注册表进行判断方法 @echo OFF reg Query "HKLM\Hardware\D ...
- 响应式自适应布局代码,rem布局
响应式自适应布局代码 首先是先设置根字体大小,PC端一般是16px为根字体,移动端会有不同的,根据情况来设置 js部分 document.querySelector('html').style.fon ...
- Activiti工作流引擎学习(一)
1.部署对象和流程定义相关表:RepositoryService act_re_deployment: 部署对象表:一次部署的多个文件的信息,对于不需要的流程可以删除和修改 act_re_procde ...
- php 上传文件并对上传的文件进行简单验证(错误信息,格式(防伪装),大小,是否为http上传)
<body> <?php /** *验证错误 *如果有错,就返回错误,如果没错,就返回null */ function check($file) { //1:验证是否有误 if($f ...
- boostrap-非常好用但是容易让人忽略的地方【2】:row
row是非常好用但是却非常容易忽略的地方. 想实现内部元素相对父级的padding=0,则在父子中间加个row.如下图 列嵌套也是同样的道理 经验之谈:学会row的用法,在手机版布局的时候会很方便,否 ...
- visio基础
右下角是一个切换文件的按钮 也可以用ctrl+tab键进行切换 页面底部左边是一个页面的增加与切换的几个按钮 这是切换页面不是切换文件 右上角这个按钮是一个功能隐藏的按钮 左上角这个按钮可以自定义快速 ...
- .data()与.detach()的区别
.data()和.detach()都可以获取Variable内部的Tensor,但.detach()更加安全 https://zhuanlan.zhihu.com/p/38475183
- pytorch代码调试工具
https://github.com/zasdfgbnm/TorchSnooper pip install torchsnooper 在函数前加装饰器@torchsnooper.snoop()
- [梁山好汉说IT] 容器概念在北宋社会的应用
[梁山好汉说IT] 容器概念在北宋社会的应用 0x00 摘要 如何对没有软件开发经验的人解释容器? 集装箱真的能够完美解释容器嘛? 除了集装箱还有其他常见实体能够解释容器嘛? 我找到了一个能够 和集装 ...
