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 ...
随机推荐
- Core Animation演示
相关代码展示: - (IBAction)toggleRoundCorners:(id)sender { [CATransaction setDisableActions:![_enableAnimat ...
- React中 checkbox 与 label 标签的搭配
用<label>标签替代checkbox的点击样子,点击<label>实际上就是点击checkbox checkbox的checked值会跟着一起变 <input typ ...
- (34)zabbix Queue队列
概述 queue(队列)显示监控项等待刷新的时间,可以看到每种agent类型刷新时间,通过queue可以更好的体现出监控的一个指标.正常情况下,是一片绿色. 如果出现过多红色,那么需要留意一下.我们也 ...
- The Three Day
函数基础-练习 #.写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作 # def modify_file(filename,old,new): # import os # w ...
- Web框架之Django_02基本操作(Django项目启动配置、数据库连接、orm、增删改查)
摘要: Django项目简单现实过程 pycharm连接数据库 Django之orm简单操作增删改查 一.新建Django项目.配置.设置: 新建Django项目:(为了熟悉Django操作,暂时全部 ...
- LeetCode(92) Reverse Linked List II
题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1- ...
- windows10系统下安装keras框架以theano为后端并配置gpu加速
在安装之前,请确保你的显卡是NVIDIA的,并且是以下型号,否则不能进行gpu加速,右键我的电脑--管理--设备管理器--显示适配器.另外如果你的电脑是windows7,安装教程也是一样的,不过根据k ...
- IntelliJ IDEA 类和方法注释的生成以及Javadoc的简单使用记录
idea,设置类注释和,方法注释的常见的设置方法(不同的版本设置方法有所偏差,简单记录一些目前自己在使用的方法,) 方法注释:在keyMap中搜索Fix doc comment ,后点击右键设置一个快 ...
- CMDB资源配置管理项目
浅谈ITIL TIL即IT基础架构库(Information Technology Infrastructure Library, ITIL,信息技术基础架构库)由英国政府部门CCTA(Central ...
- luogu3755 [CQOI2017]老C的任务
扫描线水题. #include <algorithm> #include <iostream> #include <cstdio> using namespace ...