Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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的更多相关文章

  1. 【leetcode刷提笔记】Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. 【leetcode刷提笔记】Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  3. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  4. 刷题11. Container With Most Water

    一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...

  5. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  6. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  7. leetcode第11题--Container With Most Water

    Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...

  8. 【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).  ...

  9. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

随机推荐

  1. ios 使用gcd 显示倒计时

    __block ;//倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ...

  2. Windows键

    Windows键+D: 最小化或恢复windows窗口   Windows键+U: 打开“辅助工具管理器”    Windows键+CTRL+M 重新将恢复上一项操作前窗口的大小和位置 Windows ...

  3. 在Excel2003中给定区域填充随机数?

    1.选中一个单元格,输入“=RAND()*40+60”,点CTRL+ENTER键,即可完成公式填充. 2.鼠标悬停单元格边框右下角,出现“+”符号,左键拉动实现格式复制,从而填充整个区域随机数. ★题 ...

  4. PHP接收和发送XML数据(json也通用)

    一.接收xml数据, 使用php://input,代码如下: <?php $xmldata=file_get_contents("php://input"); $data=s ...

  5. Codeforces 34C-Page Numbers(set+vector+暴力乱搞)

    C. Page Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. 什么是CouchDB?

    ※本文对CouchDB的解释是来自Apache CouchDB的官网的译文,如果有什么问题,请指正. 1.CouchDB简介 CouchDB是一种利用JSON文件,javascript作为MapRed ...

  7. Word 操作

    1.出文件,最后一页是附件.最后一页的页码不想要.如何删除?用的是 office word 2010版本,跟07 03版本界面不一样. 在最后一页的最前面插入分节符:下一页 ,编辑页脚.让页脚“取消链 ...

  8. [译]GLUT教程 - 创建和关闭子窗体

    Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Creating and Destroying Subwind ...

  9. linux head-common.s分析(转)

    供head.S调用,其中__mmap_switched的b start_kernel跳转到C执行,且永不返回. 跳转到start_kernel时寄存器值: R0 = cp#15 control reg ...

  10. 天兔插件监控mysql

    Lepus3.8-天兔mysql数据库监控系统搭建 原创ixhao2016-08-24 02:36:23评论(11)3183人阅读 Lepus3.8-天兔mysql数据库监控系统搭建 lepus是一款 ...