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). ...
随机推荐
- C#中virtual(虚方法)的理解以及和abstract(抽象方法)的区别
Virtual方法(虚方法) virtual 关键字用于在基类中修饰方法.virtual的使用会有两种情况: 情况1:在基类中定义了virtual方法,但在派生类中没有重写该虚方法.那么在对派生类实例 ...
- Yearning v1.3.0 发布,Web 端 SQL 审核平台
企业级MYSQL web端 SQL审核平台. Website 官网 www.yearning.io Feature 功能 数据库字典自动生成 SQL查询 查询工单 导出 自动补全,智能提示 查询语句审 ...
- Will Georgia Tech's $7K online M.S. in computer science program make the grade?
https://newatlas.com/georgia-tech--graduate-computer-science-degree-mooc/28763/ Georgia Tech to offe ...
- fedora27安装后的配置工作(持续更新)
换源 没什么可说的,安装后更换国内软件源是必须做的事,推荐更换阿里的镜像源.换源教程 添加epel源 EPEL (Extra Packages for Enterprise Linux)是基于Fedo ...
- SQLSERVER无排序生成序号
实现方式:ROW_NUMBER() SELECT RowID=(ROW_NUMBER() OVER(ORDER BY(SELECT ))) FROM dbo.tbl_name 实现方式:IDENTIT ...
- Alpha事后诸葛亮(阳光普照队)
Alpha事后诸葛亮 设想和目标 1.实现文字识别,以用户喜欢的图片做背景将其保存,生成新的图片. 2.时间比较赶,主要是因为队员对于Android开发方面的了解不多,可以说是几乎没有,需要一步一步的 ...
- leetcode 1. Two Sum [java]
注意点: HashMap<Integer, Integer> return new int[]{}; 3 2 4 target:6 return null; public int[] tw ...
- svn: 无法连接主机“127.0.0.1”: 拒绝连接
尝试搭建SVN服务器,前面的步骤都OK,服务已经启动, svnserve -d --listen-port 80 --listen-host 0.0.0.0 -r /data/nerrissa/svn ...
- python第四十课——构造函数
1.动态给对象添加属性: 在对象创建完毕后,单独为其添加需要的属性:可以理解为:私人定制 [注意]: 添加的属性只有此对象能够使用,别的对象如果用了,直接报错; 2.构造函数/构造方法/构造器: 格式 ...
- splay专题复习——bzoj 3224 & 1862 & 1503 题解
[前言]快要省选二试了.上次去被虐出翔了~~这次即便是打酱油.也要打出风採! 于是暂停新东西的学习,然后開始复习曾经的知识.为骗分做准备.PS:区间翻转的临时跳过,就算学了也来不及巩固了. [BZOJ ...