【LeetCode每天一题】Container With Most Water(容器中最多的水)
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
Example: Input: [1,8,6,2,5,4,8,3,7] Output: 49
思路
我们可以设置两个指针,一个指向开始位置,一个指向尾部。然后求出当前储水量,然后将短的一方向前移动,然后继续算储水量。并且和之前的进行比较。大于替换,否则直接下一步。 时间复杂度O(n), 空间复杂度为O(1)。
图示步骤




代码
class Solution(object):
def maxArea(self, height):
if len(height) < : # 长度小于2直接返回
return
start, end = , len(height)-1
areas =
while start < end: # 开始循环比较
tem = min(height[start], height[end]) # 选出最小的数字
areas = max(areas, tem*(end - start)) # 与之前的储水量进行比较
if height[start] < height[end]: # 选择移动的指标
start +=
else:
end -=
return areas
【LeetCode每天一题】Container With Most Water(容器中最多的水)的更多相关文章
- leetcode第11题--Container With Most Water
Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...
- [LeetCode每日一题]153.寻找旋转排序数组中的最小值
[LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...
- LeetCode OJ Container With Most Water 容器的最大装水量
题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 【LeetCode two_pointer】11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode 笔记系列二 Container With Most Water
题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- LeetCode(11) Container With Most Water
题目 Given n non-negative integers a1, a2, -, an, where each represents a point at coordinate (i, ai). ...
- 【LeetCode每天一题】Trapping Rain Water(获得雨水的容量)
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
随机推荐
- vmware的centos 6虚拟机如何共享文件夹?
1.点 虚拟机-设置 2. 点击 选项-共享文件夹: 3.点击添加,选择 windows主机的一个文件夹作为 共享文件夹: 5.点击完成-确定以后,重启虚拟机(客户机),在虚拟机中: 点击 compu ...
- 60款很酷的 jQuery 幻灯片演示和下载【转】
jQuery 是一个非常优秀的 JavaScript 框架,使用简单灵活,同时还有许多成熟的插件可供选择,它可以帮助你在项目中加入漂亮的效果,其中之一就是幻灯片效果的实现,这是一种在有限的网页空间内展 ...
- SSM项目实战
1. 实战才是检验学的怎么样的标准,一个小项目,运行老是出错,加上自己一贯的马虎的习惯,不严谨,就使学习之路更加的曲折了,感觉自己在这一行中比较吃力,但是自己选择了这条路,就得好好走下去,不要怀疑自 ...
- web开发之环境配置和文件系统
web开发中有jsp,html,css,java,pictures等文件和程序,怎么组织他们,使其正确加载,是一个比较大的问题,就像一团乱麻,解不开啊.IDE是个大管家,要对它非常熟悉才可以,跟顺利地 ...
- linux 下实用软件工具推荐
截图:Deepin-Screenshot 音乐:deepin-music netease-music 绘图工具:Draw.io Desktop (chrome extension) / www.pro ...
- db2 查询表前几行
不需要那么多不需要那么多数据的时候使用fetch first xxx rows only数据的时候使用fetch first xxx rows only
- 蓝桥杯 入门训练 Fibonacci数列 解析
问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n ...
- 制作dlib(面部识别检测)静态库
参考或引用网址: 快速精准的人头检测,代码已开源http://url.cn/5e3qQPN http://www.jianshu.com/p/701e8dea887e 官网文档 GitHub地址 1. ...
- 怎样使用 fiddler抓取网络数据包?
今天我们使用的工具是一个非常著名的抓包工具,百度搜索一下即可找到(或者关注/私信我,查看共享,一般我在百度经验中使用到的软件类工具,都可以在共享网盘中找到),因此这里不演示下载,相信您能很容易得到它的 ...
- python摸爬滚打之day01----初识Python
1.编程语言分类 编译型语言:程序被一次性全部翻译成机器语言,计算机直接以机器语⾔言来运⾏行行此程序. 优点:运行效率高,可脱离语言环境独立运行. 缺点:开发效率低,可移植性差. 解释型语言:将程序逐 ...