Container With Most Water 双指针法
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.
双指针
一种枚举算法的优化,在具有有序性质的问题上可以优化复杂度,此题题意是在数组中选取两条边和X轴一起组成矩形容器,最多能存储多少水,影响容量的因素有两个: 容器的底(左右两条线段长度之差)和高(左右两条线段长度最小值)。
双指针在数组首尾相向移动, 优化掉大部分的无效计算([3,1,3,3] 这个数组,左右指针分别在首尾,左边的指针向右移动,底和高都会减少,面积显然不会增大,这一步计算可以直接优化掉)
class Solution {
public:
int maxArea(vector<int>& height) {
int ans = 0, l = 0, r = height.size() - 1;
if(!height.empty()) {
ans = max(ans, (r - l) * min(height[l], height[r]));
while(l < r){
if(height[l] < height[r]){
while(l < r && height[l+1] < height[l]){
l++;
}
l++;
}
else {
while(l < r && height[r - 1] < height[r]){
r--;
}
r--;
}
ans = max(ans, (r - l) * min(height[l], height[r]));
}
}
return ans;
}
};
Container With Most Water 双指针法的更多相关文章
- [Swift]LeetCode11. 盛最多水的容器 | 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). ...
- 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 ...
- C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3615 访问. 给定 n 个非负整数 a1,a2,...,an,每 ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 67. Container With Most Water
Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
随机推荐
- Java简答题附答案
1. Java有没有goto? 有, Goto语句在java中作为保留字, 并没有实现它. 带标号的break, continue局限于循环体中跳转 带标号的goto可以在一个函数(c语言)中任意跳转 ...
- python之编码的进阶
识记点: ascii 不支持中文 gbk 国标 中文2 英文1 unicode 万国码 英文2 中文4 utf-8 英文1 欧洲2 亚洲3 硬盘中存储的是字节 用什么编码就用什么解码 # 一段文字的转 ...
- 初涉tarjan缩点
tarjan缩点:口胡过好多题,不过从来没写过…… 什么是缩点 tarjan和Kosaraju.Gabow算法一样,是为了求有向图中的强连通分量.因为有向图中大多数情况下会有环存在,而有环是一个不甚好 ...
- 【Java_基础】java类加载过程与双亲委派机制
1.类的加载.连接和初始化 当程序使用某个类时,如果该类还未被加载到内存中,则系统会通过加载.连接.初始化三个步骤来对类进行初始化.如果没有意外,jvm将会连续完成这三个步骤,有时也把这三个步骤统称为 ...
- mycat中间件安装与使用
前提: 安装JDK版本在7.0及其以上 1.下载: 下载地址在:http://dl.mycat.io/ 选择1.6-release版本下载 2.安装: 直接解压即可: tar -zxf Mycat-s ...
- Ubuntu 下的aegisub安装
大家用开源的软件用习惯了.推荐大家使用一下字幕编辑软件 分享的是Ubuntu下的安装教程: $ sudo add-apt-repository ppa:djcj/aegisub $ sudo apt- ...
- Java8特性详解 lambda表达式 Stream【转】
本文转自http://www.cnblogs.com/aoeiuv/p/5911692.html 1.lambda表达式 Java8最值得学习的特性就是Lambda表达式和Stream API,如果有 ...
- 解析Java finally
以下用几个简单的例子介绍一下finally的用法: 例子1 public class Test { public static void main(String[] args) { System.ou ...
- 如何创作用纯 CSS 绘制一支栩栩如生的铅笔
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/PaZYBw 可交互视频教 ...
- CSS3-文本-text-overflow
text-overflow 语法: text-overflow : clip | ellipsis 取值说明: 1.clip:表示不显示省略标记(...),而只是简单的裁切,需要在一定的高度范围内配合 ...