【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 ...
随机推荐
- M - Pots
You are given two pots, having the volume of A and B liters respectively. The following operations c ...
- 遍历DOM打平
html 模板 <div class="box"> <p>1</p> <p>2</p> <div> < ...
- C#打印标签
一个复杂的标签包括一个复杂的表格样式和二维码.条形码等内容.所以如果直接绘制的方式将会非常的麻烦,所以采用使用的方案是使用模板的方式:1.使用Excel创建出想要的模板的样式.2.对模板中的动态内容进 ...
- easyui tree 更改图标
easyui tree 更改图标 ,onLoadSuccess: function (node, data) { $('#tt .tree-icon').css("background&qu ...
- .NET Core开发日志——简述路由
有过ASP.NET或其它现代Web框架开发经历的开发者对路由这一名字应该不陌生.如果要用一句话解释什么是路由,可以这样形容:通过对URL的解析,指定相应的处理程序. 回忆下在Web Forms应用程序 ...
- 品尝阿里云容器服务:5个2核4G节点使用情况记载
使用5台2核4G非IO优化的ECS作为节点创建集群,节点操作系统是Ubuntu 16.04.2 LTS.创建后3个为mananger节点,2个为worker节点,每个节点默认会运行7个容器,其中3个s ...
- ubuntu16.04安装kinetic调用gazebo_control解决方案
解决方案 sudo apt-get install ros-kinetic-gazebo-ros-control
- php字符串操作
1.字符串的格式化 按照从表单提交数据之后,php处理的不同:接受,显示,存储.也有三种类型的格式化方法. 1.1字符串的接收之后的整理: trim(),ltrim(),rtrim() 当数据从表单中 ...
- 一个列转行SQL示例(wm_concat函数和replace函数合用)
准备测试数据: create table test01( groupid number, a number, b number, c ...
- day5:字典dict
1, 判断是不是列表 li = ['lis3a', 'mary', 'lucy', 'hh', 'kk', 'gg', 'mm', 'oo', 'vv'] if type(li) == list: p ...