11.Container With Most Water---两指针
题目链接:https://leetcode.com/problems/container-with-most-water/description/
题目大意:给出一串数组(a1, a2, a3, ...an),表示坐标(i, ai),同样表示一条直线是从(i, 0)到(i, ai),从中选出两条直线,计算其容器能蓄水多少,并找出最多的蓄水量。(容器不能倾斜,就是两条直线可以是不同高度,但是蓄水量要按照低高度的那条直线来计算),例子如下:

法一:暴力,对于一条直线,分别遍历左边和右边直线,计算其蓄水量,取最大者。可惜超时了。代码如下:
public int maxArea(int[] height) {
int res = 0;
for(int i = 0; i < height.length; i++) {
//计算左边直线
for(int j = height.length - 1; j > i; j--) {
if(height[j] >= height[i]) {
res = Math.max(res, (j - i) * height[i]);
break;
}
}
//计算右边直线
for(int j = 0; j < i; j++) {
if(height[j] >= height[i]) {
res = Math.max(res, (i - j) * height[i]);
break;
}
}
}
return res;
}
法二:两指针移动,与42题的两指针移动类似但略有区别,左指针与右指针相比较,记录较大者,若左指针较小,则从左往右移动,若右指针较小,则从右往左移动,如果当前值比较大者大,则计算:(较大者下标-当前值下标)*当前值高度,计算得到最后的结果。代码如下(耗时8ms):
public int maxArea(int[] height) {
int res = 0, left = 0, right = height.length - 1;
while(left < right) {
//如果左指针<=右指针
if(height[left] <= height[right]) {
while(left < right && height[left] <= height[right]) {
res = Math.max(res, (right - left) * height[left++]);
}
}
//如果左指针>右指针
else {
while(left < right && height[left] > height[right]) {
res = Math.max(res, (right - left) * height[right--]);
}
}
}
return res;
}
11.Container With Most Water---两指针的更多相关文章
- leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II
11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- 刷题11. Container With Most Water
一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [leecode]---11.container with most water
description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- leetcode problem 11 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). ...
随机推荐
- Unbuntu+nginx+mysql+php
1/准备 sudo su --切换到root 2/nginx安装 apt-get update apt-get install nginx 3/mysql 安装 apt-get install mys ...
- 洛谷 P1231 教辅的组成(网络最大流+拆点加源加汇)
题目背景 滚粗了的HansBug在收拾旧语文书,然而他发现了什么奇妙的东西. 题目描述 蒟蒻HansBug在一本语文书里面发现了一本答案,然而他却明明记得这书应该还包含一份练习题.然而出现在他眼前的书 ...
- Halum UVA - 11478(差分约束 + 二分最小值最大化)
题意: 给定一个有向图,每条边都有一个权值,每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的边的权值增加d,最后要让所有边权的最小值非负且尽量大 两个特判 1 ...
- bzip2 --安装
下载源文件安装包: http://www.bzip.org/downloads.html 解压: tar -xzvf bzip2-1.0.6.tar.gz 进入解压后的目录: cd bzip2-1.0 ...
- webstorm下搭建编译less环境 以及设置压缩css
webstorm自带less,不过要编译的话需要nodejs环境. 首先去node的主页下载对应版本的nodejs然后安装,下载地址:http://nodejs.org/ 安装完之后打开命令提示符(w ...
- PID控制算法的C语言实现五 积分分离的PID控制算法C语言实现
在普通PID控制中,引入积分环节的目的主要是为了消除静差,提高控制精度.但在过程的启动.结束或大幅度增减设定时,短时间内系统输出有很大的偏差,会造成PID运算的积分积累,致使控制量超过执行机构可能允许 ...
- Codeforces Round #209 (Div. 2)A贪心 B思路 C思路+快速幂
A. Table time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- 全表 or 索引
这一篇文章证实了以前对MySQL优化程序的工作原理. MySQL就像一个人一样,总是聪明的去选择当前最快的方式去查询,而不是像Oracle数据那样死板地根据规格去查询. 查询的要求在于快.而对于数据库 ...
- centos7下安装mysql5.7.24
第一步:下载rpm包 sudo wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-rel ...
- echart图跟随屏幕自适应变化
var myChart = echarts.init(document.getElementById('main'),'macarons');// var option = { //...一些配置 / ...