LeetCode Container With Most Water (Two Pointers)
题意
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.
就是说在x轴上有一堆竖线,要选出两根来,让它们形成的容器能装最多的水。
解法
看到Tag里的提示是Two Pointers
,这个标签在CF里也经常看到,以前不知道是什么意思,这次学了一下,就是用两个指针来找解的办法,比如在排好序的数组里找出两个数让他们的和等于某个值,就可以在数组左右两端各放一个指针,如果他们指向的值的和大于要求的值就向左移动右指针使值变小,反之就向右移动左指针使值变大。
在这题里,同样在数组的左右两边设置两个指针,这时候容器的宽是最大的,任何移动都会使宽变小,所以如果要将容积增大,唯一的做法就是增加高,所以如果某个点的值小于指针指向的两个值中最小的那个,那么就可以略过。
class Solution
{
public:
int maxArea(vector<int>& height)
{
int len = height.size();
int l = 0,r = len - 1;
int temp = 0,ans = 0;
int min_height = 0;
while(l < r)
{
min_height = min(height[l],height[r]);
temp = min_height * (r - l);
ans = ans > temp ? ans : temp;
while(height[l] <= min_height && l < len)
l ++;
while(height[r] <= min_height && r >= 0)
r --;
}
return ans;
}
};
LeetCode Container With Most Water (Two Pointers)的更多相关文章
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- LeetCode 11. [👁] Container With Most Water & two pointers
盛最多水的容器 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode]Container With Most Water, 解题报告
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...
- [Leetcode] Container With Most Water ( C++)
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- C++LeetCode:: Container With Most Water
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...
- leetcode Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] Container With Most Water 简要分析
前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...
- LeetCode——Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- sql server 时间格式转换
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- MySQL自带的性能压力测试工具mysqlslap
mysqlslap是从MySQL的5.1.4版开始就开始官方提供的压力测试工具. 通过模拟多个并发客户端并发访问MySQL来执行压力测试,同时提供了较详细的SQL执行数据性能报告,并且能很好的对比多个 ...
- HTTP的cookie
HTTP cookies,通常又称作"cookies",已经存在了很长时间,但是仍旧没有被予以充分的理解.首要的问题是存在了诸多误区,认为cookies是后门程序或病毒,或压根不知 ...
- 【转】Java学习---Java中volatile关键字实现原理
[原文]https://www.toutiao.com/i6592879392400081412/ 前言 我们知道volatile关键字的作用是保证变量在多线程之间的可见性,它是java.util.c ...
- JDK5 新特性之 可变参数的方法(2)---asList
> Arrays.asList(T - a)方法的使用 >UnsupportedOperationException分析 Arrays.asList(T - a)方法的使用 pac ...
- 当DATABASE进入了suspect模式以后
一个VM的错误就造成了sql2012的脱序.很多一般看不到的模式陆续登场 诸如 recovery pending, suspect, EMERGENCY. 以下脚本可以帮助恢复,如果文件没有损坏的话. ...
- bootstrap datepicker含有hasDatepicker无法弹出
bootstrap datepicker 初始化时,会给控件添加hasDatepicker类 ,如果此时调用 $singleDay.datepicker(initDayOpts);无法弹出时间控件 需 ...
- 【转】jquery cookie操作
Cookie是网站设计者放置在客户端的小文本文件.Cookie能为用户提供很多的使得,例如购物网站存储用户曾经浏览过的产品列表,或者门户网站记住用户喜欢选择浏览哪类新闻. 在用户允许的情况下,还可以存 ...
- BZOJ 1934 善意的投票
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1934 题目大意: 幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉.对他们来说,这个问 ...
- 20145236《网络对抗》Exp1 逆向及Bof基础
20145236<网络对抗>Exp 1逆向及Bof基础 一.实践目标 运行原本不可访问的代码片段 强行修改程序执行流 以及注入运行任意代码. 二.基础知识及实践准备 理解EIP寄存器及其功 ...