LeetCode--No.011 Container With Most Water
11. Container With Most Water
- Total Accepted: 86363
- Total Submissions: 244589
- Difficulty: 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.(不能倾斜容器)
思路:
这道题很简单,大概意思是要找到两条条纵线然后这两条线以及X轴构成的容器能容纳最多的水。而任意两条线与x轴一起能装的水的最大量就是两条线在x轴的距离乘以较矮的那条线的高度即可。所以解题思路就很简单了。
方法一:
暴力求法,求每一种情况下的最大装水面积,然后比较是否最大,不是则下一种,是则替换为当前最大值。
public int maxArea2(int[] height) {
int n = height.length ;
if(height == null || n < 2){
return 0 ;
}
int max = 0 ;
for(int i = 0 ; i < n ; i++){
for(int j = i+1 ; j < n ; j++){
int low = height[i]<height[j]?height[i]:height[j] ;
int area = low*(j-i)/2 ;
max = max>area?max:area ;
}
}
return max ;
}
方法二:
最大盛水量取决于两边中较短的那条边,而且如果将较短的边换为更短边的话,盛水量只会变少。所以我们可以用两个头尾指针,计算出当前最大的盛水量后,将较短的边向中间移,因为我们想看看能不能把较短的边换长一点。这样一直计算到左边大于右边为止就行了。
注意:如果将较短的边向中间移后,新的边还更短一些,其实可以跳过,减少一些计算量
public int maxArea(int[] height) {
int n = height.length ;
if(height == null || n < 2){
return 0 ;
}
int max = 0 ;
int left = 0 ;
int right = n-1 ;
while(left < right){
int low = height[left]<height[right]?height[left]:height[right] ;
int area = low*(right-left) ;
max = max>area?max:area ;
if(low == height[left]){
left++ ;
}else{
right-- ;
}
}
return max ;
}
LeetCode--No.011 Container With Most Water的更多相关文章
- 【LeetCode】011 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
- No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
- 【JAVA、C++】LeetCode 011 Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [Leetcode]011. Container With Most Water
public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; ...
- LeetCode(11)题解: Container With Most Water
https://leetcode.com/problems/container-with-most-water/ 题目: Given n non-negative integers a1, a2, . ...
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
随机推荐
- oralce 中union 和union all 的简单使用说明
union:对两个结果集进行合并操作:不包括重复行:同时进行默认规则的排序: union all:对两个结果集进行合并操作:包括重复行:不排序:
- Scrollview包裹布局问题。
输入框获取焦点,键盘弹出,背景图片上移: https://blog.csdn.net/wljian1/article/details/79962802 android:scrollbarThumbVe ...
- Python设计模式 - 基础 - 封装 & 继承 & 多态
面向对象的核心是对象,世间万物都可以看作对象,任何一个对象都可以通过一系列属性和行为来描述,可以包含任意数量和类型的数据或操作.类是用来描述具有相同属性和方法的所有对象的集合.类通常是抽象化的概念,而 ...
- oracle 查询索引和主键
ORACLE: 1.查主键名称: select * from user_constraints where table_name = 'AAA' and constraint_type ='P'; 查 ...
- DBHelper类
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import ja ...
- 使用安装脚本安装Texlive
介绍 TeX Live 是 TUG (TeX User Group) 维护和发布的 TeX 系统,可说是「官方」的 TeX 系统.网上可找到的教程大多是从镜像安装完整版texlive.镜像发布的周期较 ...
- Linux知识扩展一:执行前为什么加./
转载:https://www.cnblogs.com/fortunel/p/8663669 1 ./表示当前路径,在执行可执行文件时,linux系统会从环境变量PATH中查找该文件的路径,但因为 L ...
- node.js 使用 net 模块模拟 websocket 握手,进行数据传递。
websocket 是一种让浏览器与服务器之间建立持久的连接,并能进行双向数据传输的一种协议. websocket 属性应用层协议,基于tcp传输协议,并复用http的握手通道. 一.如何进行webs ...
- css3回顾 checkbox
<div class="checkBox"> <input type="checkbox" id="check1"> ...
- linux就该这么学,第十一天了
今天讲了,网卡绑,定,两块网卡同时工作,自动备源,理论上速度提升一倍,工作中可以用到的技术 还有sshd服务,端口22,远程连接使用,还可以设置root是否可以直接登录,主要配置文件在,/etc/ss ...