题目:

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.

题意:

这道题的要求是给定整形数组a,(i, ai)表示坐标点,这些坐标点向x轴作垂直的线。找到两条线,使其和x轴共同构成的容器,可以装下最多的水。(注意,不允许倾斜容器)

每次做题都尽量先自己写,当看到acc时还是蛮开心的,然后看看别人写的程序,总是会有一种顿悟的感觉。

我本人的解题思路:

想到的最简单的方法是从左到右遍历一下给定的数组,利用两重循环O(n2)来实现,这样很明显会超时。接下来就想想什么地方可以优化一下:第一层循环从左到有遍历数组,第二层循环可以从右到左遍历一下,找到大于左边left的高度时就可以用break跳出本次循环了,因为容器的最大高度是被left限制了的,而此时高度达到了最大,且(j-i)是最大的,就找到了对于每个left的最大容积,写出代码后提交一下,发现最后一组测试数据超时了(还是可以有优化的地方),考虑了一下第二层循环已经优化了,那就看看第一层循环吧,在从左到右遍历的过程中如果当前left的height小于之前的某一个left的hright,那么就可以用continue跳过本次循环了,因为容器的最大高度比之前的要小,而且(j-i)也肯定小了,最大的容积肯定不会出现在当前的left。

优化之后的代码通过了测试,就去看了看别人的代码,发现自己从右到左优化了,从左到右优化了,就是没有想到利用两个指针,分别从左、右开始遍历(想想很衰)

先贴一下自己写的代码:

public class Solution {
public int maxArea(int[] height) {
int maxa = 0;
int maxi = 0; //存从左到右遍历过程中最高的left
if(height.length<2)
return 0;
for(int i=0;i<height.length-1;i++){
if(height[i]<height[maxi]){
continue;
}
for(int j=height.length-1;j>i;j--){
if(height[j]>height[i]){
int maxmid = (j-i)*height[i];
maxa = maxa>maxmid?maxa:maxmid;
break;
}
else{
int maxmid = (j-i)*height[j];
maxa = maxa>maxmid?maxa:maxmid;
}
}
maxi = i;
}
return maxa;
}
}

这里先说说利用两个指针从两边向中间遍历的大致思路:

先找距离最长的两个位置看能装下多少水,随后缩小两边的距离,因为距离减少,所以要想使得所能容纳的水变多,只能提高形成的“桶”的高度。因为高度与最低的边界一致,所以缩小边界时要从值小的那一边找,知道找到大于“桶高”的值,说明桶的高度可以得到提升,计算新桶所能容纳的体积并与最大值比较。以此类推,直到两边不能再缩小位置。

这种思路大致理解一下还是可以的,但是自己总是感觉有某种假设不能满足,也没办法很好的说明,就将自己理解过程中画的几种情况列出来

验证的过程就不多写了,感觉要写好多····

代码:

public class Solution {
public int maxArea(int[] height) {
int maxa = 0;
int i = 0,j=height.length-1;
while(i<j){
int maxmid;
if(height[i]<height[j]){
maxmid = (j-i)*height[i];
i++;
}
else{
maxmid = (j-i)*height[j];
j--;
}
maxa = maxa>maxmid?maxa:maxmid;
}
return maxa;
}
}

这里的代码还是可以由优化的地方的就是当前的left小于之前的left的时候就不要求maxa了。

 

Leetcode Array 11 Container With Most Water的更多相关文章

  1. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  3. LeetCode OJ 11. Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  4. 【LeetCode】11. Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  5. leetcode problem 11 Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  6. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  7. LeetCode Array Medium 11. Container With Most Water

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

  8. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  9. leetcode面试准备:Container With Most Water

    leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...

随机推荐

  1. vue基础教程

    1.执行npm install 2.安装stylus,(npm install之后node_module已经有了stylus,但还是要再安装一次) npm install --save-dev sty ...

  2. LESS CSS使用方法 , CSS也能跟JS一样玩

    本文转自  http://blog.csdn.net/xuyonghong1122/article/details/51986472 在使用CSS的时候,总会有这个想法 这个属性值老是重复写好烦 这个 ...

  3. KMP--君住长江头,我住长江尾,日日思君不见君,共饮长江水

    POJ 3461: Oulipo 题意: 求出第一个串在第二个串中的出现次数... 分析: KMP板子题... 代码: #include<algorithm> #include<io ...

  4. superagent-promise

    var Promise = this.Promise || require('promise'); var agent = require('superagent-promise')(require( ...

  5. Codeforces 583 DIV2 Asphalting Roads 模拟

    原题链接:http://codeforces.com/problemset/problem/583/A 题意: 很迷很迷,表示没看懂..但是你看样例就秒懂了 题解: 照着样例模拟就好 代码: #inc ...

  6. POJ 3710 Christmas Game [博弈]

    题意:略. 思路:这是个删边的博弈游戏. 关于删边游戏的预备知识:http://blog.csdn.net/acm_cxlove/article/details/7854532 学习完预备知识后,这一 ...

  7. hosts不支持泛解析

    hosts不支持泛解析,只能是一个域名对应一个IP. 如果想要实现只能用一些第三方的DNS软件做解析.

  8. SQLite to Asp.net Entity Framework 部署问题

    最近做了一个小应用,使用SQLite做数据库.开始用DBLINQ的时候,做一个LINQ查询出现不支持的问题.后来看到Entity Framework是可以支持SQLite的,于是很快转换过来.完成开发 ...

  9. IOS开发~开机启动&无限后台运行&监听进程

    非越狱情况下实现: 开机启动:App安装到IOS设备设备之后,无论App是否开启过,只要IOS设备重启,App就会随之启动: 无限后台运行:应用进入后台状态,可以无限后台运行,不被系统kill: 监听 ...

  10. hdu1862

    //开始把student stu[100000]放置在main()中导致栈溢出,所以必须放在全局位置, //可以调用数组的排序函数sort,包含头文件#include<algorithm> ...