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. tf.train.string_input_producer()

    处理从文件中读数据 官方说明 简单使用 示例中读取的是csv文件,如果要读tfrecord的文件,需要换成 tf.TFRecordReader import tensorflow as tf file ...

  2. dijkstra堆优化(multiset实现->大大减小代码量)

    例题: Time Limit: 1 second Memory Limit: 128 MB [问题描述] 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣 ...

  3. 【t056】智力问答(multiset做法)

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 新年联欢会上,G.Sha负责组织智力问答节目.G.Sha建立了一个很大很大的超级题库,并衡量了每道题的 ...

  4. Vue+node.js实现一个简洁的个人博客系统

    本项目是一个用vue和node以及mysql实现的一个简单的个人博客系统,整体逻辑比较简单.但是可以我们完整的了解一个项目从数据库到后端到前端的实现过程,适合不太懂这一块的朋友们拿来练手. 本项目所用 ...

  5. <sUbjeCt>Reverse aAlignment SemInaR

    翻译过来就是有关逆序对问题的专题. 因为大胆报名担任学校专题讲师所以跪着也要准备好课件...那什么是逆序对? 逆序对就是序列中ai>aj且i<j的有序对 举个栗子: 其中,5>4,但 ...

  6. 【12.78%】【codeforces 677D】Vanya and Treasure

    time limit per test1.5 seconds memory limit per test256 megabytes inputstandard input outputstandard ...

  7. Rancher2.x部署K8s

    1.安装Docker [root@localhost ~]# docker -v Docker version , build 774a1f4 2.使用Docker运行Rancher : stable ...

  8. Android1_运行第一个AS项目HelloWorld

    一.开发安卓程序需要具备一些开发工具,这里简单罗列一下: JDK :这是Java语言的开发工具包,包含了Java的运行环境.工具集合.基础类库等内容. Android Studio:目前主流的安卓开发 ...

  9. 023.MFC_属性页控件(tab control)

    属性页控件属性页->选项卡->对话框CTabCtrl一.建立名为tabCtrl的mfc工程,添加Tab Control控件,设置属性ID为IDC_TAB,并添加变量m_tab 在tabCt ...

  10. PLsql下载官网下载地址

    https://www.allroundautomations.com/registered/plsqldev.html