题目:

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。

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

先贴一下自己写的代码:

  1. public class Solution {
  2. public int maxArea(int[] height) {
  3. int maxa = 0;
  4. int maxi = 0; //存从左到右遍历过程中最高的left
  5. if(height.length<2)
  6. return 0;
  7. for(int i=0;i<height.length-1;i++){
  8. if(height[i]<height[maxi]){
  9. continue;
  10. }
  11. for(int j=height.length-1;j>i;j--){
  12. if(height[j]>height[i]){
  13. int maxmid = (j-i)*height[i];
  14. maxa = maxa>maxmid?maxa:maxmid;
  15. break;
  16. }
  17. else{
  18. int maxmid = (j-i)*height[j];
  19. maxa = maxa>maxmid?maxa:maxmid;
  20. }
  21. }
  22. maxi = i;
  23. }
  24. return maxa;
  25. }
  26. }

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

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

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

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

代码:

  1. public class Solution {
  2. public int maxArea(int[] height) {
  3. int maxa = 0;
  4. int i = 0,j=height.length-1;
  5. while(i<j){
  6. int maxmid;
  7. if(height[i]<height[j]){
  8. maxmid = (j-i)*height[i];
  9. i++;
  10. }
  11. else{
  12. maxmid = (j-i)*height[j];
  13. j--;
  14. }
  15. maxa = maxa>maxmid?maxa:maxmid;
  16. }
  17. return maxa;
  18. }
  19. }

这里的代码还是可以由优化的地方的就是当前的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. linux系统——etc下的passwd 文件

    etcpasswd 文件 在登陆时要求输入用户名和密码,就是根据这个来的. root::0:0:root:/root:/bin/bash bin:x:1:1:bin:/dev/null:/bin/fa ...

  2. spring+freemarker+redis

    1./pom.xml文件内容: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...

  3. Java代码添加背景音乐

    太心塞!弄了很久才终于把Java添加背景音乐实现了.不过还是很Happy! 这次介绍的办法,是只要一打开Java Application,便可直接听到背景音乐.代码保存,方便以后再次利用. packa ...

  4. Python之文件操作:经验总结

    1.怎么判断读出来的文件是gbk还是utf-8编码 if content == u'中国'.encode('gbk'):     return 'gbk' elif content == u'中国'. ...

  5. 控制cxGrid 主从表的明细只展开一个

    procedure TForm.ADetailDataControllerCollapsing( ADataController: TcxCustomDataController; ARecordIn ...

  6. group by timestamp

    SELECT DATE_FORMAT( deteline, "%Y-%m-%d %H" ) , COUNT( * )  FROM test GROUP BY DATE_FORMAT ...

  7. Javascript&Ajax-深入浅出JSONP--解决ajax跨域问题

    Javascript&Ajax-深入浅出JSONP--解决ajax跨域问题 原理讲解: 链接地址:http://www.cnblogs.com/chopper/archive/2012/03/ ...

  8. XPath gramma

    XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) 来选取的. XML 实例文档 我们将在下面的例子中使用这个 XML 文档. & ...

  9. AC日记——琪露诺 洛谷 P1725

    琪露诺 思路: 单调队列+dp: 然而劳资不会单调队列,所以,线段树水过; 来,上代码: #include <cstdio> #include <cstring> #inclu ...

  10. vuex 表单字段映射工具 vuex-map-fields

    vuex在处理表单的时候显得很麻烦,要一个字段一个字段的去写set和get还有mutation,字段多的话带来的工作量将是非常巨大的.vuex-map-fields将能很好的解决这个问题. vuex- ...