[leecode]---11.container with most water
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的更多相关文章
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- 刷题11. Container With Most Water
一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
随机推荐
- C# 求链表 list 中 属性的 最大值 最小值
获取链表List中对象属性最大值最小值(Max,Min)的方法: 1.创建一个类,类中有一个属性A /// <summary> /// 用于测试属性的类 /// </summary& ...
- 多个for循环嵌套会影响速度
在复现Meta-SR的过程中,发现如果嵌套多个for 循环会使速度过慢.这是下面实验得出的结论: import time t1 = time.time() a = range(3000) b = ra ...
- 打return
var zz=xx(); alert(zz); zz=yy(); alert(zz); function xx(){ var i=1,j=2; return i+j; } function yy(){ ...
- 融云携新版实时音视频亮相 LiveVideoStack 2019
4 月 19 日,LiveVideoStack 2019 音视频大会在上海隆重开幕,全球多媒体创新专家.音视频技术工程师.产品负责人.高端行业用户等共襄盛会,聚焦音频.视频.图像.AI 等技术的最新探 ...
- python-支付宝支付示例
项目演示: 1.输入金额 2.扫码支付: 3.支付完成: 4.跳转回商户 一.注册账号 https://openhome.alipay.com/platform/appDaily.htm?tab= ...
- GHSpro文件上传
GHSpro文件上传 文章 GHSpro 文件上传 基本设计图 上传 上传方式的选择 采用iframe 的表单上传 ajaxfileupload <div id="step-2&quo ...
- (转)在Eclipse中进行C/C++开发的配置方法(20140721最新版)
因准备考试原因需要在windows下配置C++标准运行环境,找到此文,Mark之. 先列举下自己遇到的情况: 1 JRE安装不上,点了exe文件后没有反应: 安装JDK!!! 2 Eclipse找 ...
- Unity用Excel.dll简单读取Excel内容
Unity用Excel.dll简单读取Excel内容 需要Excel.dll 需要如下三个命名空间 using System.IO; using Excel; using System.Data; 1 ...
- Linux 管理环境变量的文件分为系统级和用户级别
Linux 管理环境变量的文件分为系统级和用户级别 管理环境变量的文件也分为系统级和用户级别: 1.系统级:/etc/profile:该文件是用户登录时,操作系统定制用户环境时使用的第一个文件,应用于 ...
- 误删除(重命名)libc.so.6的处理方法
LD_PRELOAD=/lib64/libc-2.12.so ln -s /lib64/libc-2.12.so /lib64/libc.so.6 ssh尚未断开的话可以直接这样操作,断开之后可以用l ...