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. mongoDB 使用总结

    http://blog.chinaunix.net/space.php?uid=22270773&do=blog&id=19935171. 如果无特别用途,建议:表名,字段名 全部用小 ...

  2. visual studio 2010 LNK1123解决方式

    ------------------------------------------------------------Lysen----------------------------------- ...

  3. [LeetCode]Palindrome Number 推断二进制和十进制是否为回文

    class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...

  4. Atitit. Atiposter 发帖机版本历史 编年史

    Atitit. Atiposter 发帖机版本历史 编年史 V1  初步实现sina csdn cnblogs V2  实现qzone sohu 的发帖功能  顺便重构接口实现分离 V3多文件循环发帖 ...

  5. IIS7设置默认页

    一般用ASP.NET创建的网站默认页都是Default.aspx,不需要设置. 但是如果有网站的起始页不是Default.aspx,就需要在IIS里设置了. IIS7的设置方法和IIS6的不一样: 在 ...

  6. laravel学习之路3 数据库相关

    读写分离之多个读? 有 'host' => $readHosts[array_rand($readHosts)], 上面的好像有缓存问题php artisan config:cache ] ); ...

  7. 四边形不等式优化DP——石子合并问题 学习笔记

    好方啊马上就要区域赛了连DP都不会QAQ 毛子青<动态规划算法的优化技巧>论文里面提到了一类问题:石子合并. n堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的 ...

  8. xcode ERROR ITMS

    1.ERROR ITMS-90046 /90085: "Invalid Code Signing Entitlements. Your application bundle's signat ...

  9. React Native安装步骤

    先贴出中文网安装指南:http://reactnative.cn/docs/0.46/getting-started.html 本文会点出一些安装时遇到的坑,和解决方案! 1.首先是安装Chocola ...

  10. LNMP环境搭建(二:MySQL)

    1.获取MySQL官方的rpm包,根据操作系统与需要安装的MySQL版本进行选择,官方地址:https://www.mysql.com/downloads/ # cd /usr/local/src # ...