装最多水的容器

给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai)。画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)(i, 0)。找到两条线,使得其与 x 轴共同构成一个容器,以容纳最多水。

解题

不理解题意

通俗的说

在上面n个点中找到两个点,使得线段一:(i,ai)  (i,0), 线段二:(j,aj)  (j,0) 线段三:(i, 0) (j, 0) 这三个线段能够组成的容器 可以盛放最多的水

二分法,找到一个区间求出最大面积

 public class Solution {
/**
* @param heights: an array of integers
* @return: an integer
*/
public int maxArea(int[] A) {
// write your code here
if(A == null || A.length <2)
return 0;
int Max = 0;
int left = 0;
int right = A.length -1;
int subMax = 0;
while(left < right){
if(A[left] < A[right]){
subMax = (right - left)*A[left];
left++;
}else{
subMax = (right - left)*A[right];
right--;
}
Max = Math.max(subMax,Max);
} return Max;
}
}

Java Code

 class Solution:
# @param heights: a list of integers
# @return: an integer
def maxArea(self, A):
# write your code here
if A == None or len(A)<2:
return 0
Max = 0
subMax = 0
low = 0
high = len(A) - 1
while low< high:
if A[low] <A[high]:
subMax = (high - low)*A[low]
low+=1
else:
subMax = (high - low)*A[high]
high-=1
Max = max(Max,subMax)
return Max

Python Code

lintcode:装最多水的容器的更多相关文章

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

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

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

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

  3. [LintCode] Container With Most Water 装最多水的容器

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

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

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

  5. LeetCode第十一题-可以装最多水的容器

    Container With Most Water 问题简介:通过一个给定数组,找出最大的矩形面积 问题详解:给定一个数组,包含n个非负整数a1,a2,…,an,其中每个表示坐标(i,ai)处的点,绘 ...

  6. 22.Container With Most Water(能装最多水的容器)

    Level:   Medium 题目描述: Given n non-negative integers a1, a2, ..., an , where each represents a point ...

  7. lintcode-383-装最多水的容器

    383-装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0 ...

  8. LeetCode:盛最多水的容器【11】

    LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为  ...

  9. Java实现 LeetCode 11 盛最多水的容器

    11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...

随机推荐

  1. mysql字段累加concat

    update tablename set field1=concat(field1,'_bak') where field2 like '%@xxx’

  2. WCF note1

    Summary of WCF Client to use service use ChannelFactory to create proxy to use service. Client code ...

  3. 关于如何将Excel数据导入到SQL Server中

    面对大量的Excel数据我们可能会非常苦恼,如果一条一条的插入到数据库:不仅会耗大量的时间,而且还可能会发生错误,现在我来说一下如何导入数据! 1.准备工作 首先要在Excel中建立数据表对应的数据字 ...

  4. sqlite与C++进行连接

    SQLite数据库是零配置的,sqlite数据库不同于SqlServer等数据库,SQLite不需要复杂配置,只需要,将SQLite的库文件和动态链接文件拷贝到相应工程目录下,就可以使用SQLite数 ...

  5. 利用while(code!=EOF){}来实现“无限”循环

    #include <stdio.h>int main(){    char a,b,c;    char t;    while(scanf("%c%c%c",& ...

  6. net分布式系统架构

    net分布式系统架构的思路 最近看到有部分招聘信息,要求应聘者说一下分布式系统架构的思路.今天早晨正好有些时间,我也把我们实际在.net方面网站架构的演化路线整理一下,只是我自己的一些想法,欢迎大家批 ...

  7. UITableView基本使用和cell的属性

    在ios的UI中UITableView是个常用且强大的控件 基本使用: 1>设置代理,一般把控制器设为代理:self.tableView.delegate = self; 2>遵守代理的协 ...

  8. Bootstrap入门三:页面排版

    在Bootstrap中,页面的排版都是从全局的概念上出发,定制了主体文本.强调文本.标题.Code风格.按钮.表单.表格等格式,并运用CSS3的@font-face和伪元素一起实现了一套icon主题. ...

  9. .net之XML

    前言 想想毕业到现在已经工作了3个月,总结的知识点还是太少.这周因为项目完成的差不多了,有空补充一下知识,上周学了XML吗,一直都没有总结.今天便总结一下,一方面梳理一下知识点,一方面巩固下知识. X ...

  10. foj 2044 1 M possible 二进制压缩

    题目链接: http://acm.fzu.edu.cn/problem.php?pid=2044 题意:  给出 一大堆数,找出2个出现次数模3 为1 的两个数字   题解: 把一个数分为几位拆开统计 ...