Container With Most Water——LeetCode
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.
题目大意:给定一个数组,下标和值分别代表x轴坐标和高度,找出两个元素使得他们之间和x轴围成的范围最大(由短的那个决定高)。
解题思路:一开始想暴力,后来想DP,应该都是O(N^2)的,都过不了,后来看了下提示Two Pointers,前后两个指针,与2Sum的思想类似。
看的比较好的一个解释:
Idea / Proof:
- The widest container (using first and last line) is a good candidate, because of its width. Its water level is the height of the smaller one of first and last line.
- All other containers are less wide and thus would need a higher water level in order to hold more water.
- The smaller one of first and last line doesn't support a higher water level and can thus be safely removed from further consideration.
- public int maxArea(int[] height) {
- if(height == null || height.length<=1){
- return 0;
- }
- int area=Integer.MIN_VALUE,i=0,j=height.length-1;
- while(i < j){
- area = Math.max(area,(j-i)*Math.min(height[i],height[j]));
- if (height[i]<height[j]) {
- i++;
- }else{
- j--;
- }
- }
- return area;
- }
Container With Most Water——LeetCode的更多相关文章
- Container With Most Water - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Container With Most Water - LeetCode 注意点 没什么好注意的... 解法 解法一:暴力求解,假设任意两个端点会是最佳答 ...
- Container With Most Water -- LeetCode 11
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Container With Most Water leetcode java
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ Container With Most Water 容器的最大装水量
题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...
随机推荐
- OSI七层模型理解
物理层功能1,为数据端设备提供传送数据的通路 功能2,传输数据 接口.传输介质.信号的传输.网络设备 有线介质:双绞线(普通的网线),光纤. 无线介质:无线电.微波.激光.红外线. 例如手机.电视接收 ...
- Hibernate 的HQL语句,初级
这里讲解简单的HQL语句,因为很多比较复杂的外查询,用一般的查询很难完成 所以这里需要使用HQL @Test public void selquery(){ System.out.printf(&qu ...
- CSS3条件判断——@supports/window.CSS.supports()(转)
CSS3条件判断,听起来"不明觉厉",如果你对CSS稍为熟悉一点的话,你会发现CSS中的"@media"就是条件判断之一.是的,在CSS3的条件判断规范文档中包 ...
- java String对象的创建(jvm).
本人目前也开始学习虚拟机,在java中,有很多种类型的虚拟机,其中就以sum公司(当然现在已经是oracle了)的虚拟机为例,介绍可能在面试的时候用到的,同时对自己了解String有很大帮助,这里仅仅 ...
- 无软驱加载raid驱动安装windows2003及其他微软操作系统
[转载]http://blog.zol.com.cn/2650/article_2649199.html [另一篇]http://www.blue1000.com/bkhtml/c159/2013-0 ...
- xargs rm -rf 与 -exec rm
# find ./ -exec rm {} \; # find ./ | xargs rm -rf 两者都可以把find命令查找到的结果删除,其区别简单的说是前者是把find发现的结果一次性传给exe ...
- inno setup 多语言安装
之前的安装程序默认语言为英文,现在我们需要将它变成中文,由于InnoSetup安装包中默认没有带中文语言文件,我们需要下载一个先: 到http://www.400gb.com/u/758954/123 ...
- ZOJ 刷题记录 (。・ω・)ノ゙(Progress:31/50)
[热烈庆祝ZOJ回归] P1002:简单的DFS #include <cstdio> #include <cstring> #include <algorithm> ...
- POJ2104 K-th Number 静态区间第k最值 平方分割
干掉这道题的那一刻,我只想说:我终于**的AC了!!! 最终内存1344K,耗时10282ms,比起归并树.划分树以及其他各种黑科技,这个成绩并不算光彩⊙﹏⊙ 但至少,从最初的无数次TLE到最终的AC ...
- CSS的inherit与auto使用分析
一个很显浅的寓言,千年老树,电打雷劈,屹立不倒,却毁于蝼蚁的侵袭之下.自以为精通CSS的人,常常被一些小问题搞到头晕脑胀. 通常是一个很小的数值,经过层层放大歪曲后,整个布局就走形了.CSS是一门很简 ...