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) ...
随机推荐
- c/c++常用代码 -- 共享内存
#pragma once #include <stdio.h> #include <tchar.h> #include <string.h> #include &l ...
- 14.quartus联合modelsim仿真
在quartus调用modelsim仿真过程中,出现了一个错误,如下所示: Check the NativeLink log file I:/Quartus11.0/Myproject/testi_n ...
- oracle11g创建数据库最后一步确定时弹出无法创建目录
总的说是Windows7的权限问题 Windows7 dos命令下输入dbca创建数据库,因为权限问题,数据库将无法完成.所以还是在开始程序中打开dbca创建数据库比较好. Windows7 dos以 ...
- 关于1>LINK : fatal error LNK1168: 无法打开 ....exe或者....dll进行写入的问题
我们用VS编译器运行我们的程序时候,可能会出现关于1>LINK : fatal error LNK1168: 无法打开 ...dll 进行写入或者是1>LINK : fatal err ...
- iptables规则表
1.iptables规则表 Filter(针对过滤系统):INPUT.FORWARD.OUTPUT NAT(针对地址转换系统):PREROUTING.POSTROUTING.INPUT.OUTPUT ...
- Android -- PowerManager和PowerManager.WakeLock
PowerManager.WakeLock PowerManager.WakerLock是我 ...
- LeetCode-Implement strStr()-KMP
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- 集成Spring后HibernateTemplate实现分页
spring 整合 hibernate 时候用的 HibernateTemplate 不支持分页,因此需要自己包装一个类进行分页,具体实现如下...使用spring的hibernateTemplate ...
- Windows窗体应用程序(非Console)使用libuv实现简单的异步WEB服务器
libuv是一个很强大的异步处理框架(严格意义上不能叫框架,其实就是一组异步函数库,当然框架这东西有各种各样的定义和理解_^...),最初的的目的是用于NODEJS的异步处理,不过因为它是一个独立的项 ...
- Noip2015总结
Noip2015战役总结 [游记部分] Day0 考前说是可以放松一下,下午呢就在机房打了几盘杀,一起玩了玩狼人.不过晚上觉得还是要有点氛围了,于是稍稍打了几个模板,觉得正确率还不错,给自己一点自信的 ...