description:

Input: [1,8,6,2,5,4,8,3,7]Output: 49

思路1:

  从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2

思路2:

  找出数组中最大的数,将其与次大,第三大数求面积,依次类推,也需要两层循环,还需要额外排序,时间复杂度n2

  因为找出最大数并且并不知道输入数据的规律(有可能很杂乱),所以每个都有必要算,采取思路1.

  代码实现如下:

class Solution {
public int maxArea(int[] height) {
int max = 0;
for(int i = 0; i < height.length; i++){
int mid = maxSpare(height, i);
max = max >= mid ? max : mid;
}
return max;
}
// 从index = start开始向后对每一对求面积
private int maxSpare(int[] height, int start){
int maxArea = 0;
for(int i = start + 1; i < height.length; i++){
int area = countArea(height[start], height[i], i - start);
maxArea = maxArea > area ? maxArea : area;
}
return maxArea;
}
private int countArea(int height1, int height2, int width){
int result = 0;
int height = height1 > height2 ? height2 : height1;
result = height * width;
return result;
}
}

but only faster 15.40% of Java online submissions for Container With Most Water.

讨论区看见O(n)的算法:

(1)left = 0, right = n-1,计算left~right之间的面积

(2)若left的高度小于right的高度,则查找2~n-1之间大于left的位置,更新left,计算面积;否则,查找中间大于right高度的位置,作为新right,计算面积

(3)直到right< left

思考:

  设以left=1为基点不变,找到一个right值,使left~right之间的矩形面积最大,则存在以下三种情况:

①aleft > aright

  此时高度height=aright,width=(n-1),所求面积可能小于等于以a1为左边的最大面积。

②aright为右边起第一个大于等于aleft的边,设right=n-1

  此时height=aleft,width= (n-1)-1,面积有可能等于为以a1为左边的最大面积。

③aright不为右边起第一个大于等于aleft的边,设right=n-2

  此时虽然height=aleft,但是width=(n-2)-1小于②中的width,所以③中求得的面积小于②中的面积,即③中的情况是不考虑的

  所以可以两头推进。参考如下代码:

class Solution {
public int maxArea(int[] height) {
int max = 0, left = 0, right = height.length-1;
while(left < right){
int area = Math.min(height[left],height[right]) * (right - left);
max = max > area ? max : area;
if(height[left] < height[right]){
int base = height[left];
while((height[left] <= base) && (left < right)){
left++;
} // end while
}
else{
int base = height[right];
while((height[right] <= base) && (left < right)){
right--;
} // end right
} // end if-else
} // end while
return max;
} // end maxArea
}
Runtime: 7 ms, faster than 55.28% of Java online submissions for Container With Most Water.

算法很重要 

一个3ms的代码:

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

[leecode]---11.container with most water的更多相关文章

  1. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

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

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

  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 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

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

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

  7. LeetCode 11. Container With Most Water (装最多水的容器)

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

  8. [LeetCode] 11. Container With Most Water 装最多水的容器

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

  9. LeetCode#11. Container With Most Water

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

随机推荐

  1. 记录心得-IntelliJ iDea 创建一个maven管理的的javaweb项目

    熟能生巧,还是记录一下吧~ 开始! 第一步:File--New--Project--Maven--Create from archetype--maven-archetype-webapp 第二步:解 ...

  2. Tarjan求LCA

    LCA问题算是一类比较经典的树上的问题 做法比较多样 比如说暴力啊,倍增啊等等 今天在这里给大家讲一下tarjan算法! tarjan求LCA是一种稳定高速的算法 时间复杂度能做到预处理O(n + m ...

  3. Sketchup (待续)

    Sketchup插件 来自20个最好用的SketchUp插件 https://www.bilibili.com/video/av17242031/?from=search&seid=15336 ...

  4. 【JavaScript】JS知识点复习

    1.引入的两种方式:直接在标签里行内js,在body最下端引入. 2.变量的5种类型:number,string,boolean,null,undefined以及一种特殊类型:object 3.变量命 ...

  5. github文件上传与下载

    一.文件上传 ①.注册并登陆github,进入Github首页,点击New repository新建一个项目. ②.填写相应信息后点击create即可 Repository name: 仓库名称 De ...

  6. 第三方jar包上传私服和项目使用

    下面只做个人日志记录,勿喜勿喷 使用两个浏览器,带着下面的问题去看:https://www.cnblogs.com/tyhj-zxp/p/7605879.html.就会清晰了 1.下载和安装nexus ...

  7. Eclipse如何导入maven项目,以及配置maven

    Eclipse如何导入maven项目,以及配置maven 一.准备工作 1. eclipse,安装了eclipse 2. 一个需要导入的maven项目 3. 下载好了的压缩包apache-maven- ...

  8. PHP遍历二叉树

    遍历二叉树,这个相对比较复杂. 二叉树的便利,主要有两种,一种是广度优先遍历,一种是深度优先遍历. 什么是广度优先遍历?就是根节点进入,水平一行一行的便利. 什么是深度优先遍历呢?就是根节点进入,然后 ...

  9. "做中学"之“极客时间”课程学习指导

    目录 "做中学"之"极客时间"课程学习指导 所有课程都可以选的课程 Java程序设计 移动平台开发 网络攻防实践 信息安全系统设计基础 信息安全专业导论 极客时 ...

  10. Java的实例化

    实例化是什么 1.在Java中,使用已经定义好的类,创建该类对象的过程称为"实例化". 2.实例化就是在堆中分配内存的过程,分配的内存即对象. 3.只有实例化后的对象,才能访问到类 ...