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 ...
随机推荐
- HttpRunnerManager使用(一)
用例: 一.request requests---headers===>headers设置 requests---type:params===>请求参数设置,URL参数(get) requ ...
- docker镜像加速器
目前国内比较靠谱的镜像加速器网址:https://www.daocloud.io/mirror
- Lua中面向对象
一.Lua中类的简单实现: (1)版本——摘自 Cocos2.0中的: --Create an class. function class(classname, super) local superT ...
- servlet中的请求响应与重定向区别
一.概念 请求响应(转发):将客户端请求转发另一个servlet或者jsp页面------------------------getRequestDispatcher()方法 重定向: 返回一个连接给 ...
- Java虚拟机 内存区域划分
(图片来自https://www.cnblogs.com/whgk/p/6138522.html) 先从线程私有区开始介绍 虚拟机栈 Java虚拟机栈是由一个个栈帧组成的,当一个方法被调用时,代表这个 ...
- Python+Selenium学习--自动生成HTML测试报告
前言 在脚本运行完成之后,除了在log.txt 文件看到运行日志外,我们更希望能生一张漂亮的测试报告来展示用例执行的结果. HTMLTestRunner 是Python 标准库的unit ...
- FortiGate上架前准备
1.收集信息 1.网络拓扑信息(了解网络拓扑信息有助于网络方案的规划) 2.环境信息(了解部署位置.部署模式.最大吞吐.最大用户数有助于对设备性能的评估) 3.客户需求,对FortiGate部署的功能 ...
- [leetcode]256. Paint House粉刷房子(三色可选)
There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...
- pc send instructor pc ad
1. #include <string.h> #include <intrins.h> // 加入此头文件后,可使用_nop_库函数 #define MAIN_Fosc 11 ...
- tcp、ip、http
tcp是传输层协议,ip是网络层协议,http是应用层协议,简单说就是tcp是传输数据,而http是封装数据. rpc与http的区别是项目大的话,接口间调用变多的话,采用rpc的话,不用像http那 ...