【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。
题解:分别设置两个游标start和end,start从height数组的前面往后面走,end从height数组的后面往前面走。每次计算当前start和end能够形成的container的最大面积,然后将height较小的游标移动(比如如果height[start]<height[end],那么就将start往后移动一步),直到两个游标相遇,算法结束。
算法的正确性:每次移动高度较小的游标,是因为以该游标为高的最大盛水量已经计算过了。比如序列1 2 3 2 2的过程如下:
(start)1 2 3 2 2(end) answer = max(answer,1*5) = 5;此时height[start] < height[end],将start右移,因为以start=0为高的水瓶,宽度最大就是5;
1 (start)2 3 2 2(end) answer = max(answer,2*4) = 8; 此时height[start] = height[end],将start右移,因为以start=1为高的水瓶,宽度最大是4;
1 2 (start)3 2 2(end) answer = max(answer,3*3) = 9; 此时height[start] > height[end],将end左移,因为此时start左边的板高度都比end板高度低,所以此时start指向的位置就是以end为高度的水瓶宽度最远能到达的地方,即宽度最大的地方。所以以end板为高的水瓶最大的盛水量已经计算出来了,就可以把end左移了。
1 2 (start)3 2(end) 2 answer = max(answer,3*1) = 9; 此时height[start] > height[end],将end左移,start=end,循环结束。
代码如下:
public class Solution {
public int maxArea(int[] height) {
int start = 0 ;
int end = height.length-1;
int maximum = 0; while(start < end){
int width = end - start;
int h = Math.min(height[start], height[end]); maximum = maximum > width*h?maximum:width*h; if(height[start] > height[end])
end--;
else {
start++;
}
} return maximum;
}
}
【leetcode刷提笔记】Container With Most Water的更多相关文章
- 【leetcode刷提笔记】Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【leetcode刷提笔记】Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- 刷题11. Container With Most Water
一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- leetcode第11题--Container With Most Water
Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...
- 【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). ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
随机推荐
- Hibernate核心类和接口具体介绍
一.hiobernate核心类和接口预览图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGxnZW4xNTczODc=/font/5a6L5L2T/fo ...
- Android实用工具
1 json类:hiJson 格式化json字符串 2 sqlite类:sqlitespy,SQLiteExpertSetup 3
- GLSL经典新手教程汇总
权威官方文档:https://www.opengl.org/documentation/glsl/ 权威民间金典新手教程:http://blog.csdn.net/racehorse 一个具体完整的G ...
- Android服务类Service具体解析
Service有什么作用? 很多人不明确service是用来干嘛的.事实上Service作为Android四大组件之中的一个,能够理解为一个执行在后台的Activity.它适用于处理一些不干扰用户的长 ...
- pycharm下: conda installation is not found ----一个公开的bug的解决方案
pycharm conda installation is not found ----一个公开的bug的解决方案 pycharm+anaconda 是当前的主流的搭建方案,但是常出现上述问题. ...
- php 微信开发之新增上传/获取下载临时素材
php 微信开发之新增上传/获取下载临时素材 代码 <?php define("AppID","");//你的id define("AppSec ...
- Nginx 配置指令的执行顺序
在一个 location 中使用 content 阶段指令时,通常情况下就是对应的 Nginx 模块注册该 location 中的“内容处理程序”.那么当一个 location 中未使用任何 cont ...
- px与与rem vw的区别
1.px 使用具体像素点为单位,好处是比较稳定和精确,但在浏览器放大缩小会出现问题 2.rem 参考根元素的值 例如设置根元素字体大小是20像素 在h1中设置字体大小 那么H1的大小就是40px p的 ...
- Disruptor 创建过程
1 Disruptor disruptor = new Disruptor<ValueEvent>(ValueEvent.EVENT_FACTORY, ringBufferSize, ex ...
- <button>标签也能提交表单问题
如何避免<button>标签也能提交表单的问题: 只需加上一个属性:type='button'即可:如<button type="button"> < ...