lintcode:装最多水的容器
装最多水的容器
给定 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:装最多水的容器的更多相关文章
- [LeetCode] 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). ...
 - [LintCode] 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第十一题-可以装最多水的容器
		
Container With Most Water 问题简介:通过一个给定数组,找出最大的矩形面积 问题详解:给定一个数组,包含n个非负整数a1,a2,…,an,其中每个表示坐标(i,ai)处的点,绘 ...
 - 22.Container With Most Water(能装最多水的容器)
		
Level: Medium 题目描述: Given n non-negative integers a1, a2, ..., an , where each represents a point ...
 - lintcode-383-装最多水的容器
		
383-装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0 ...
 - LeetCode:盛最多水的容器【11】
		
LeetCode:盛最多水的容器[11] 题目描述 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 ...
 - Java实现 LeetCode 11 盛最多水的容器
		
11. 盛最多水的容器 给定 n 个非负整数 a1,a2,-,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) ...
 
随机推荐
- firebreath 在谷歌和火狐浏览器下的调试 以及打包
			
在寻找插件开发资料的过程中找到了一个开发浏览器插件的开源项目——firebreath firebreath的安装以及测试我就不再叙述了,可以参考大神的文章 . http://www.blogjava. ...
 - sharepoint 2010 基于AD的Form验证
			
一.新建web应用程序 1.验证部分选择“基于声明的身份验证” 2.设置端口 3.选择“启用基于窗体的身份验证(FBA)” “ASP.NET 成员身份提供程序名称”下面填写“LdapMember” “ ...
 - js从富文本中找出固定电话、手机号码、邮箱并添加上链接。
			
function richtextfindtel(value) { /*固定电话.手机号码.邮箱*/ var tempValue = value.replace(/<[^>]+>/g ...
 - JAVA数据结构-----枚举
			
枚举(Enumeration)接口虽然它本身不属于数据结构,但它在其他数据结构的范畴里应用很广. 枚举(The Enumeration)接口定义了一种从数据结构中取回连续元素的方式. 例如,枚举定义了 ...
 - LintCode-BackPack II
			
Given n items with size A[i] and value V[i], and a backpack with size m. What's the maximum value ca ...
 - Notes of the scrum meeting(11/2)
			
meeting time:13:00~13:30p.m.,November 2nd,2013 meeting place:3号公寓楼一层 attendees: 顾育豪 ...
 - 用C++编写一个随机产生多个两位数四则运算式子的简单程序
			
一 设计思想: 1.首先可以想到一个四则运算式子的组成:两个运算数和一个运算符: 2.两个运算数的随机由调用随机函数产生,其中可以设定运算数的范围: 3.一个运算符的随机产生可以分为加减乘除四种情况, ...
 - Crtmp  源码分析
			
Crtmp Server接收rtmp音视频流,并实现音视频并发,可以作为直播后台的服务.整套代码量并不大,算是轻量级的服务. 花了些时间研究源码,现将研究的结果,记录下来,方便以后查阅. 先不从架构上 ...
 - fwrite ,fprintf的作用与区别
			
1.概念和作用 fwrite是C语言函数,指向文件写入一个数据块,写入的是 fprintf是C/C++中的一个格式化写-库函数,其作用是格式输出到一个流/文件中:原型是int fprintf( FIL ...
 - 01.JSP基础语法
			
本章主要讲解Java Web与JSP的入门内容,适合有JSP或Java Web基础的读者学习. 1.Web应用与web.xml文件 (1)Java Web应用程序的结构 Java We ...