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 ...
随机推荐
- ZeroNet搭建个人网站,一些搞笑图片
ZeroNet是一个利用比特币加密和BT技术提供不受审查的网络与通信的BT平台,ZeroNet网络功能已经得到完整的种子的支持和加密连接,保证用户通信和文件共享的安全.使用ZeroNet,你可以匿名上 ...
- H3C STP配置示例
- eslint在webstorm中有错误警告
1. 报错Missing space before function parentheses的问题 解决:在代码目录中,打开.eslint文件,并在rules中添加如下一行代码即可: "sp ...
- ZR993
ZR993 首先,这种和平方有关的,首先应当考虑根号做法 这道题目,我们可以直接暴力\(\log_{10}w + 10\)判断一个数是否能够由原数变化的到 直接\(O(\sqrt{n})\)枚举所有的 ...
- Little Elephant and Array CodeForces - 220B (莫队)
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...
- git无密码push
近来项目中调研,jupyterlab和git的整合内容,git server我使用的gitbucket和bitbucket.(项目要求使用bitbucket,看错一个字母下载了两个镜像) gitbuc ...
- Openstack生产环境部署(一)
- 如何在ClickOnce 应用中使用 GitVersion
https://github.com/GitTools/GitVersion/issues/1153 I'm using GitVersion in an internal ClickOnce app ...
- Python自定义函数的参数
在Python中自定义的函数可以有三类不同的参数 formal parameters positional arguments Keyword Arguments When a final forma ...
- 清晰架构(Clean Architecture)的Go微服务: 设计原则
我最近写了一个Go微服务应用程序,这个程序的设计来自三个灵感: 清晰架构"Clean Architecture"¹ and SOLID (面向对象设计)² 设计 原则³ Sprin ...
