Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

代码如下:(方法一:超时)

 public class Solution {
public int maxArea(int[] height) {
int max=0;
for(int i=0;i<height.length-1;i++)
{
int min=height[i];
for(int j=i+1;j<height.length;j++)
{
if(min>height[j])
min=height[j];
if(min*(j-i)>max)
max=min*(j-i);
}
}
return max;
}
}

代码如下:(方法二:Accept)

 public class Solution {
public int maxArea(int[] height) { int left=0,right=height.length-1;
int max=Math.min(height[0],height[height.length-1])*(right-left); while(left<right)
{
if(height[left]<=height[right])
{
if(height[left]*(right-left)>max)
max=height[left]*(right-left);
left++;
}
else{
if(height[right]*(right-left)>max)
max=height[right]*(right-left);
right--;
}
}
return max;
}
}

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. [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: 找出数组中最大的数 ...

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

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

  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, ai). ...

  10. LeetCode#11. Container With Most Water

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

随机推荐

  1. LA 5059 - Playing With Stones

    博弈 SG  由于每个a太大,没有办法递推,但是可以找规律 a为偶数  SG(a)=a/2 a为奇数  SG(a)=SG(a/2) 代码: #include <iostream> #inc ...

  2. winform按钮和子按钮

    实现目标: 一.两组按钮1和2,其中按钮2有两个子按钮, (1)当选按钮1时,按钮2和其子按钮不选中: (2)选中按钮2或其子按钮3和4时,1不选中 (3)选中按钮2时,默认选中其子按钮3 (4)选中 ...

  3. getBoundingClientRect() 来获取页面元素的位置

    getBoundingClientRect() 来获取页面元素的位置   document.documentElement.getBoundingClientRect 下面这是MSDN的解释: Syn ...

  4. Android 读写SD卡的文件

    今天介绍一下Android 读写SD卡的文件,要读写SD卡上的文件,首先需要判断是否存在SD卡,方法: Environment.getExternalStorageState().equals(Env ...

  5. [转] lib和dll 区别,生成及使用方法

    lib 和 dll 的区别.生成以及使用详解 [目录] lib dll介绍 生成动态库 调用动态库 生成静态库 调用静态库 首先介绍一下静态库(静态链接库).动态库(动态链接库)的概念,首先两者都是代 ...

  6. classPath

    问 spring mvc的web.xml中这个地方的classpath是什么意思? spring springmvc java swnuv 2015年09月25日提问 关注 5 关注 收藏 0 收藏, ...

  7. hdu 2090

    PS:输入麻烦...我还以为是输入一个就输出一个...后来看了大神的才知道....暴力破解了... 代码; #include "stdio.h" int main(){ ,a,b; ...

  8. BZOJ 1047 理想的正方形

    单调队列的基本应用. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  9. HDOJ-三部曲-1001-Flip Game

    Flip Game Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Su ...

  10. Egret

    http://www.manew.com/forum-html5Engine-1.html http://www.manew.com/forum-html5Engine-1.html https:// ...