【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 ...
随机推荐
- cordova 插件 调用iOS社交化分享(ShareSDK:微信QQ分享)
1.github上已有的插件:https://github.com/nwpuhmz/ShareSDKPlugin 2.安装插件 cordova plugin add https://github.co ...
- Hadoop学习之pig
首先明确pig是解决什么问题而出现的,pig是为了简化mapreduce编程而设计的,并且有自己的一套脚本语言.其基本由命令和操作符来定义的,如load,store,它的功能很明确,用来大规模处理数据 ...
- J - S-Nim
Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as ...
- python 中 打印及格式化字符串的相关方法
原文 将值转换为字符串 Python 有多种方式将任何值转为字符串: 将它传给 repr() 或 str() 函数. repr() 和 str() 的区别,看几个例子: >>> pr ...
- C#打印标签
一个复杂的标签包括一个复杂的表格样式和二维码.条形码等内容.所以如果直接绘制的方式将会非常的麻烦,所以采用使用的方案是使用模板的方式:1.使用Excel创建出想要的模板的样式.2.对模板中的动态内容进 ...
- bytes和str的区别与转换
bytes和str的区别 1.英文 b'alex'的表现形式与str没什么两样 2.中文 b'\xe4\xb8\xad'这是一个汉字在utf-8的bytes表现形式 3.中文 b'\xce\xd2'这 ...
- [No0000103]JavaScript-基础课程3
在 JavaScript 中,函数的参数是比较有意思的,比如,你可以将任意多的参数传递给一个函数,即使这个函数声明时并未制定形式参数 function adPrint(str, len, option ...
- lame音频编码注意的点
1.注意每次编码的PCM数据不能太短,如果太短编码函数长期返回-1这样的错误,将导致编码器彻底失效程序奔溃的问题,经测试32000在3200下稳定,大概1/10码率是没有问题的,具体请自行查询或调试 ...
- 超级有用的15个mysqlbinlog命令
在MySQL或MariaDB中,任意时间对数据库所做的修改,都会被记录到日志文件中.例如,当你添加了一个新的表,或者更新了一条数据,这些事件都会被存储到二进制日志文件中.二进制日志文件在MySQL主从 ...
- tomcat端口设置
在tomcat安装目录下,编辑/conf/server.properties 更改对应的端口: 然后系统重启就可以了.