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. 【数据结构】算法 Maximum Subarray

    最大子数组:Maximum Subarray 参考来源:Maximum subarray problem Kadane算法扫描一次整个数列的所有数值,在每一个扫描点计算以该点数值为结束点的子数列的最大 ...

  2. 手把手教你如何安装Pycharm

    手把手教你如何安装Pycharm——靠谱的Pycharm安装详细教程     今天小编给大家分享如何在本机上下载和安装Pycharm,具体的教程如下: 1.首先去Pycharm官网,或者直接输入网址: ...

  3. Linux基础命令---uname显示计算机名称

    uname uname指令用来显示计算机硬件以及操作系统的相关信息,例如计算机名.内核版本.硬件架构. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.SUSE.o ...

  4. vue脚手架3

    跟脚手架2安装都一样,已经安装脚手架2的要执行下面的命令 ,先删除 npm uninstall vue-cli -g 或 yarn global remove vue-cli 卸载 在执行下面的命令 ...

  5. Win7 指定以某个用户运行某个程式

    登陆的是用户A,想要以用户B执行某个程式,可以在cmd命令符下执行以下语句 runas /user:Domain\UserB  /savecred notepad.exe 说明:/user:的后面即为 ...

  6. 外星人入侵游戏(python代码)

    https://github.com/ehmatthes/pcc/tree/master/chapter_12/images

  7. Vue-Router路由Vue-CLI脚手架和模块化开发 之 vue-router路由

    vue-router路由:Vue.js官网推出的路由管理器,方便的构建单页应用: 单页应用(SPA)只有一个web页面的应用,用户与应用交互时,动态更新该页面的内容:简单来说,根据不同的url与数据, ...

  8. 基于vue项目一键国际化通用方案: vue-i18n + i18n-vue-cli(命令行工具)

    鉴于公司有做的国际化需求,对于公司的vue项目,觉得页面还是挺多的.刚开始觉得很简单,就是把vue文件中的中文,替换成变量,提取成一个文件就可以了,谁知道人肉的提取的部分确实太痛苦了,而且容易出错.最 ...

  9. 爬坑系列----Redis查询key报空指针异常,而redis中确实存在该key

    现象: 1.在A方法中根据key查询一个list,可以获取到相应的值 2.在B方法中同样调用此方法,传入相同的key,查询不到值,为null,报空指针异常 起初我也一脸懵逼,到现在虽然解决了,还是不知 ...

  10. iOS __block 关键字的底层实现原理 -- 堆栈地址的变更

    默认情况下,在block中访问的外部变量是复制过去的.但是可以加上 __block  来让其写操作生效. 原理: Block 不允许修改外部变量的值,这里所说的外部变量的值,指的是栈中指针的内存地址. ...