题意

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)的更多相关文章

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

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

  2. LeetCode 11. [👁] Container With Most Water & two pointers

    盛最多水的容器 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...

  3. [LeetCode] Container With Most Water 装最多水的容器

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

  4. [LeetCode]Container With Most Water, 解题报告

    前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...

  5. [Leetcode] Container With Most Water ( C++)

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

  6. C++LeetCode:: Container With Most Water

    本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...

  7. leetcode Container With Most Water

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

  8. [LeetCode] Container With Most Water 简要分析

    前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...

  9. LeetCode——Container With Most Water

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

随机推荐

  1. Prometheus Node_exporter 之 Memory Detail Meminfo /proc/meminfo

    1. Memory Active / Inactive type: GraphUnit: bytesLabel: BytesInactive - 最近使用较少的内存, 优先被回收利用 /proc/me ...

  2. USB 相关笔记

    1分析已有代码项目 Android从USB声卡录制高质量音频-----使用libusb读取USB声卡数据 github 项目:usbaudio-android-demo usb声卡取数据项目也是参考的 ...

  3. python虚拟环境 -- virtualenv , virtualenvwrapper

    virtualenv -- python虚拟沙盒 有人说:virtualenv.fabric 和 pip 是 pythoneer 的三大神器. 一.安装 pip install virtualenv ...

  4. Python2与python3中字符串的区别

    Python2 在python中包含两种字符串类型:str和unicode,str并不是完全意义上的字符串,其实是由unicode经过编码(encode)后的字节组成的字节字符串,而unicode则是 ...

  5. WinThruster清理电脑注册表

    电脑因为安装卸载各种软件,长时间工作,越来越卡慢,很大程度上和电脑中一些老旧不用的注册表有关,一些遗留问题也成为隐患. 今天我们主要来讲一下一些不用,没用的注册表清理问题. 无意间在网上看了一个软件, ...

  6. 拉格朗日插值和牛顿插值 matlab

    1. 已知函数在下列各点的值为   0.2 0.4 0.6 0.8 1.0   0.98 0.92 0.81 0.64 0.38 用插值法对数据进行拟合,要求给出Lagrange插值多项式和Newto ...

  7. iOS 固定定位不兼容、input获取焦点后位置不对。

    第一次写博客~  大家悠着看,有则改之,无则加冕,对不对的给个回复,让我知道你的存在. 在做活动页的时候,经常会碰到一些需要弹出显示的输入框(情节前提:本人安卓机~),前天自己写的时候自己调试没问题后 ...

  8. SQLite简单使用记录

    SQLite,一种轻量级的数据库 想要使用的话首先下载安装包. https://www.sqlite.org/download.html 下载sqlite-netFx20-setup-bundle-x ...

  9. October 19th 2017 Week 42nd Thursday

    If you remember me, then I don't care if everyone else forgets. 只要你记得我,我不介意整个世界都把我遗忘了. I can't follo ...

  10. sql行列转换PIVOT与unPIVOT

    基本语法 select * from Mould pivot ( count(ID)for ProductTypeCode in ( [FC], [RCU], [RCD] )) as PVT; wit ...