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) ...
随机推荐
- extjs panel自动滚动
指定两个参数 height:600, autoScroll:true, http://bbs.csdn.net/topics/280012147
- linux CentOS 系统下如何将php和mysql命令加入到环境变量中
在Linux CentOS系统上安装完php和MySQL后,为了使用方便,需要将php和mysql命令加到系统命令中,如果在没有添加到环境变量之前,执行“php -v”命令查看当前php版本信息时时, ...
- JS跨域方法及原理
JS跨域分析判断 JS跨域:在不同域之间,JS进行数据传输或通信.比如ajax向不同的域请求数据.JS获取iframe中的页面中的值(iframe内外不同域) 只要协议.端口.域名有一个不同则 ...
- DSP28335矩阵键盘的检测
#include "DSP2833x_Device.h"#include "DSP2833x_Examples.h"char temp;void gpio_in ...
- 创建并配置Filter
创建Filter需要两个步骤: 创建FIlter处理类. web.xml文件中配置Filter. 创建Filter类 创建Filter必须实现javax.servlet.Filter接口,在该接口中定 ...
- VS中引用第三方库的方法(配置sqlite数据库)
我们在编写程序时,,不可避免的会使用第三方的库文件,很少使用源文件(.cpp),大部分是使用对类进行声明的头文件和封装了类的链接库(静态lib或动态dll),比如我们写程序用的iostream这个库, ...
- Android L Camera2 API 使用实例程序汇总
在网上发现几个使用Camera API2开发的实例程序,总结一下方便后续参考: 1.Camera2 Basic : https://github.com/googlesamples/android-C ...
- color the python console text
//install termcolor module cd \ cd python27 cd scripts pip install termcolor pip install colorama // ...
- “我爱淘”冲刺阶段Scrum站立会议7
完成任务: 大事不好,今天的任务还没有完成,没有通过xml文件通过服务器显示到软件中. 计划任务: 实现通过服务器将xml文件中的数据显示到软件中. 遇到问题: 服务器已经配好,并且解析xml文件的代 ...
- 连接db2数据库时NumberFormatException异常的解决方式
连接db2数据库时报异常:java.lang.NumberFormatException: For input string: "A" from a DB2 JDBC(JCC) j ...