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 ...
随机推荐
- WINDOWS-API:取得当前用户账户名-GetUserName
bool TFormMain::GetCurrentProcessUser(AnsiString& strUserName) { bool bRet = false; //strUserNam ...
- 二分查找 && 三分查找
LeetCode34. Find First and Last Position of Element in Sorted Array 题意:找出指定元素出现的范围,Ologn 思路:两次二分 cla ...
- java面试基础篇(二)
上一篇,我们说了一下线程和Map,或许那些太抽象,不太好理解,也不太方便记忆,我们这次说一些简单的. 1.Q:java的基本数据类型有哪些? A:四种整数类型(byte.short.int.long) ...
- Python基础篇 -- 字符串
字符串 字符串是不可变的对象,任何操作对原字符串是不会有任何影响的. 索引和切片 索引 . 索引就是下标, 下标从 0 开始, 使用[] 来获取数据 s1 = "0123456" ...
- shell脚本,如何写进度条。
[root@localhost ~]# cat jindutiao.sh #!/bin/bash #进度条 n=$((/)) N=$((/)) ` do sleep 0.01 [ $(($i%$n)) ...
- shell基础笔记1
---恢复内容开始--- 1 test命令中不能使用浮点小数值,如: A=1.444444:[ $A -gt 1 ] 2 test命令中的>或<必须转义,否则shell会把它 ...
- log4j日志输出到文件的配置
1.Maven的dependency 2.log4j.properties的配置 3.Junit的Test类 4.web.xml的配置(非必要) 5.spring的db.config的配置(非必要) ...
- docker系列之安装配置-2
1.docker安装 1.CentOS Docker 安装 Docker支持以下的CentOS版本: CentOS 7 (64-bit) CentOS 6.5 (64-bit) 或更高的版本 目前,C ...
- (原) 剑指offer--之数值的整数次方
题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 初次看题觉得这题好简单,直接用库函数power()不就行了,仔细想了想,万 ...
- Beyond Compare 30天评估期结束解决办法
打开Beyond Compare 4,提示已经超出30天试用期限制 解决方法: 1.修改文件 修改C:\Program Files\Beyond Compare 4\BCUnrar.dll ,这个文件 ...