22.Container With Most Water(能装最多水的容器)
Level:
Medium
题目描述:
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 and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
思路分析:
从两端往中间搜索,面积的计算方法为两端中较小的作为高,坐标差作为宽,然后求积就是面积,时间复杂度为O(n)。
代码:
public class Solution{
public int maxArea(int []height){
int i=0; //左端
int j=height.length-1;
int maxarea=Math.min(height[i],height[j])*(j-i);
while(i<j-1){
if(height[i]<=height[j])
i++; //为什么要i++而不是j++,因为要尽可能保留高度高的
else
j--;
int area=Math.min(height[i],height[j])*(j-i);
maxarea=Math.max(maxarea,area);
}
return maxarea;
}
}
22.Container With Most Water(能装最多水的容器)的更多相关文章
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 11. Container With Most Water[M]盛最多水的容器
题目 Given \(n\) non-negative integers \(a_1,a_2,\cdots,a_n\), where each represents a point at coordi ...
- [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:装最多水的容器
装最多水的容器 给定 n 个非负整数 a1, a2, ..., an, 每个数代表了坐标中的一个点 (i, ai).画 n 条垂直线,使得 i 垂直线的两个端点分别为(i, ai)和(i, 0).找到 ...
- [LintCode] 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)处的点,绘 ...
- 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基础知识总结17(网络编程)
端口: 物理端口:IP地址 逻辑端口:用于标识进程的逻辑地址,不同进程的标识:有效端口:0~65535,其中0~1024系统使用或保留端口. JAVA中ip对象:InetAddress. import ...
- 本博文将一步步带领你实现抽屉官网的各种功能:包括登陆、注册、发送邮箱验证码、登陆验证码、页面登陆验证、发布文章、上传图片、form验证、点赞、评论、文章分页处理以及基于tronado的后端和ajax的前端数据处理。
本博文将一步步带领你实现抽屉官网的各种功能:包括登陆.注册.发送邮箱验证码.登陆验证码.页面登陆验证.发布文章.上传图片.form验证.点赞.评论.文章分页处理以及基于tronado的后端和ajax的 ...
- 数论Keynote
[同余] 1.整数a,b对模m同余的充分与必要条件是m|(a-b),即a=b+mt,t是整数. 2.性质丁.若a1=b1(mod m),a2=b2(mod m),则(a1+a2)=(b1+b2)(mo ...
- Spring总结三:DI(依赖注入)
简介: 所谓的依赖注入,其实是当一个bean实例引用到了另外一个bean实例时spring容器帮助我们创建依赖bean实例并注入(传递)到另一个bean中,比如你使用Spring容器创建的对象A里面需 ...
- java Web servlet简介及其生命周期
Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态Web内容. 我们可 ...
- 用C/C++扩展你的PHP
PHP取得成功的一个主要原因之一是她拥有大量的可用扩展.web开发者无论有何种需求,这种需求最有可能在PHP发行包里找到.PHP发行包包括支持各种数据库,图形文件格式,压缩,XML技术扩展在内的许多扩 ...
- 【总结整理】关于GrowingIO、友盟、google analysis等数据分析
作者:纯银V链接:https://www.jianshu.com/p/394ec6c96c98來源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处. 埋点主要分为四步:第一步是 ...
- springboot @Value 类中读取配置文件 .properties null 原因和解决方案
问题:在一个工具类中,通过@Value来映射配置文件的值,得到的总是null 原因:不能用new工具类的方式,应该是用容器注册(@Autowried)的方式使用此工具类,就能得到配置文件里的值 上代码 ...
- Entity Framework edmx(mapping文件)
<?xml version="1.0" encoding="utf-8"?><edmx:Edmx Version="2.0" ...
- ibatis和hibernate
ibatis:sql需要自己写hibernate:sql自动生成上面是最大的区别,下面是一些细节.选择Hibernate还是iBATIS都有它的道理:Hibernate的特点:Hibernate功能强 ...