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.

给定n个非负整数a1,a2,……,an,每一个代表坐标(i,ai)上的一个点,n条直线的两端各自是(i,ai) ,(i,0)。找出两条直线,使其与x轴形成一个容器,使容器包括最多的水。

注意:你不能够倾斜容器。

原题比較直白的解释大概如此:数组中的每一个元素代表一个水槽的边。找出两条边。使得水槽的体积最大。

这里不用考虑宽度,仅仅要 底 x 高 最大就可以。即两个水槽边的距离要越远。边的高度要越大,可是依据“木桶原理”。此处由高度较小的边决定。

就可以由两边向中间搜索,找到使得面积最大的两条边。

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

版权声明:本文博主原创文章,博客,未经同意不得转载。

LeetCode——Container With Most Water的更多相关文章

  1. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

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

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

  3. [LeetCode]Container With Most Water, 解题报告

    前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...

  4. C++LeetCode:: Container With Most Water

    本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...

  5. leetcode Container With Most Water

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

  6. [LeetCode] Container With Most Water 简要分析

    前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...

  7. [Leetcode] Container With Most Water ( C++)

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

  8. LeetCode Container With Most Water (Two Pointers)

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

  9. [Leetcode] container with most water 最大水容器

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

随机推荐

  1. Android 保存用户偏好设置

    很多情况下都允许用户根据自己的习惯和爱好去设置软件,而我们需要保存这些设置,可以用一个专业保存用户偏好的类:SharedPreferences. 这个类是实现方法其实也就是创建和修改 XML 文件, ...

  2. Apriori算法实现

    Apriori算法原理:http://blog.csdn.net/kingzone_2008/article/details/8183768 import java.util.HashMap; imp ...

  3. String[255]在高版本Delphi里还是被解释成Byte,总体长度256,使用StrPCopy可以给Array String拷贝字符串(内含许多实验测试)

    学了好多不了解的知识: procedure TForm1.Button1Click(Sender: TObject); var s1 : String; s2 : String[]; begin s1 ...

  4. Lucene.Net 2.3.1开发介绍 —— 四、搜索(二)

    原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(二) 4.3 表达式用户搜索,只会输入一个或几个词,也可能是一句话.输入的语句是如何变成搜索条件的上一篇已经略有提及. 4.3.1 观察 ...

  5. iOS - NSLog的使用方法

    NSLog的定义 NSLog定义在NSObjCRuntime.h中,如下所示: void NSLog(NSString *format, …); 基本上,NSLog很像printf,同样会在conso ...

  6. Eclipse插件引入jar包的方法(转)

    搞了两天,终于找到解决办法了.原来  Eclipse 插件项目引入外面的jar包不能用   build path---->add external jars的方法. 先说明两个概念:类加载器,O ...

  7. 【剑指offer】和为定值的两个数

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/24933341 题目描写叙述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的 ...

  8. 类型自动转换引起的误解——QString可以赋值数字1,也能直接与0比较,真是昏倒!

    看以下代码,能编译通过: void MainWindow::on_pushButton_clicked() { QString str = "test"; ) { QMessage ...

  9. [Windows Phone]修改应用程序主题

    问题: Windows Phone模拟器默认情况下是黑色(Dark)主题,开发者往往都是在黑色主题下进行应用开发,加入自定义的颜色,样式等等,而当把手机操作系统主题设为白色(Light)主题之后会发现 ...

  10. python glob 用通配符查找指定目录中的文件 - 开源中国社区

    python glob 用通配符查找指定目录中的文件 - 开源中国社区 python glob 用通配符查找指定目录中的文件