题目:

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. Eclipse 无法查看第三方jar包文件源代码解决方法

    1.打开第三方依赖包,源文件的快捷键:ctrl + mouseClick 2.由于我们下载的第三方jar 包,如Spring等相关的依赖包时,并没有附加下载相应的源文件,所以经常出现如图的这种问题. ...

  2. Bzoj3652 大新闻

    Time Limit: 10 Sec  Memory Limit: 512 MBSec  Special JudgeSubmit: 215  Solved: 112 Description Input ...

  3. 洛谷 P 2756 飞行员配对方案问题

    题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外 ...

  4. soc与cpu区别

    soc(System on Chip)片上系统cpu只包括运算器和控制器.早期 的系统是指在PCB上有cpu和Nand控制器,LCD控制器之类的控制器,构成一个系统. cpu和这些控制器之间用pcb板 ...

  5. JSTL获取Session的ID与获取文件的真实路径与项目名称

    今天在测试集群配置的时候想到session共享,因此想要获取sessionID,可以通过下面方法: ${pageContext.session.id} 获取文件的真实路径: <%=request ...

  6. glRectf(-0.5f, -0.5f, 0.5f, 0.5f)

    http://bbs.csdn.net/topics/370049656 x向右,y向上时OPENGL坐标系,z向屏幕外表示正方向(-0.5,-0.5)是左下角坐标,(0.5,0.5)是右上角坐标,, ...

  7. android中与Adapter相关的控件----GridView

    GridView(网格视图)讲解 一.GridView(网格视图)这个是控件也是比较多,和listView的很多地方都是一样的,但是GridView可以显示多列,而listView只能显示一列,个人觉 ...

  8. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---17

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  9. C++ 找不到方法标识符

    其实原因是这个CPP并没有面向对象的结构. 所以进行编译时是“顺序编译”的,而main函数的定义又在A的定义之前.自然找不到标识符了.

  10. 将Map<String, List<Map<String,Object>>>进行排序

    首先我贴上我的代码,刚开始我也不知道怎么排序还写了一些方法,最后请教群里的大神解决了 public Map<String, List<Map<String,Object>> ...