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 双指针法的更多相关文章

  1. [Swift]LeetCode11. 盛最多水的容器 | Container With Most Water

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  2. [LeetCode]11. Container With Most Water 盛最多水的容器

    Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...

  3. 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 ...

  4. C#LeetCode刷题之#11-盛最多水的容器(Container With Most Water)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3615 访问. 给定 n 个非负整数 a1,a2,...,an,每 ...

  5. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  6. [LintCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  7. 67. Container With Most Water

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

  8. LeetCode:Container With Most Water,Trapping Rain Water

    Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...

  9. No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

随机推荐

  1. OpenCV2:第八章 界面事件

    一.简介 OpenCV中提供了程序界面中的鼠标和键盘事件 二.鼠标事件 //  设置鼠标回调函数 void setMouseCallback ( const string& winname, ...

  2. Python基础篇 -- if while 语句

    2.7 if语句 # 单纯if if 条件: 代码块 当条件成立,执行代码块 # 二选一 if 条件: 代码块1 else: 代码块2 #当条件为真,执行代码块1,否则执行代码块2 # 多选一 没有e ...

  3. CAS (Compare and Swap)

    synchronized是悲观锁 注意:实现了CAS的有原子类(AtomicInteger,AtomicLong,等等原子类) CAS 是乐观锁,一种高效实现线程安全性的方法 1.支持原子更新操作,适 ...

  4. 题解 P1189 SEARCH

    (传送门)[https://www.luogu.org/problemnew/show/P1189] 先反省一波:我以后再也不用getchar()+scanf了(日常爆零) 算是比较裸的搜索吧,在下用 ...

  5. 【JavaScript】两种常见JS面向对象写法

    基于构造函数 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = function() { ...

  6. React初识整理(二)--生命周期的方法

    React生命周期主要有7中: 1. componentWillMount() :组件将要挂载时触发 ,只调用1次 2. componentDidMount() :组件挂载完成时触发,只调用1次 3. ...

  7. free指令的说明

    CentOS 6.x系统中的freefree [-b|-k|-m|-g|-h] [-l] [-o] [-t] [-s delay] [-c count] [-V] -b #-k,-m,-g 以单位by ...

  8. day14 迭代器,生成器,函数的递归调用

    1.什么是迭代器 迭代是一个重复的过程,但是每次重复都是基于上一次重复的结果而继续 迭代取值的工具 2.为什么要用迭代器 迭代器的优点 ​ ①不依赖于索引取值 ​ ②更节省内存 缺点: ​ 1.不如按 ...

  9. (转)iOS 最佳实践

    本文转自http://www.jianshu.com/p/b0bf2368fb95 感谢作者和译者 iOS最佳实践 iOS最佳实践 译者注 本文翻译自 futurice 公司的 iOS Good Pr ...

  10. nrf52810学习笔记——三

    在开发nRF52系列的蓝牙方案的时候,会用到IDE.SDK.softdevice.nrfgoStudio等开发软件,这里做一个小小的总结. 首先,下载SDK,里面有适合keil4号iar7(iar8也 ...