题意

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. linux 下查看外网ip

    1. curl ipinfo.io ~/codes/qt_codes/qt-5.4.1-build$ curl ipinfo.io{  "ip": "114.241.21 ...

  2. Cisco ASA 使用ASDM 配置管理口 方法

    CISCO ASA防火墙ASDM安装和配置 准备一条串口线一边接台式机或笔记本一边接防火墙的CONSOLE 接口,通过CRT或者超级终端连接ASA在用ASDM图形管理界面之前须在串口下输入一些命令开启 ...

  3. django -- uwsgi+nginx部署

    一. 安装nginx How To Install Nginx on CentOS 7 添加epel扩展仓 sudo yum install epel-release 安装Nginx yum inst ...

  4. CVE-2013-2551

    目录 小白的CVE-2013-2551 分析 & 利用 0xFF 前言 0x00 环境和工具 0x01 分析POC POC 调试 0x02 利用 构造R3任意内存读写 劫持eip 利用利用 0 ...

  5. pt-table-checksum工具MySQL主从复制数据一致性

    所使用的工具是pt-table-checksum 原理是: 在主上执行检查语句去检查 mysql主从复制的一致性,生成 replace 语句,然后通过复制传递到从库,再通过update 更新 mast ...

  6. sql点滴43—mysql允许用户远程登陆

     方法1 局域网连接mysql报错: ERROR 1130: Host '192.168.0.220' is not allowed to connect to this MySQL server 解 ...

  7. Django商城项目笔记No.5用户部分-注册接口-短信验证码

    Django商城项目笔记No.4用户部分-注册接口-短信验证码 短信验证码也保存在redis里(sms_code_15101234567) 在views中新增SMSCodeView类视图,并且写出步骤 ...

  8. poi 创建excel数据

    public static void main(String[] args) throws Exception { // TODO 设置excel的标题 List<String> exce ...

  9. top,ps查看进程使用内存情况

    ps -e -o 'pid,comm,args,pcpu,vsz,stime,user,uid' |grep chrome|grep -v grepwatch 'ps -e -o 'pid,comm, ...

  10. 2、Pyspider使用入门

    1.接上一篇,在webui页面,点击右侧[Create]按钮,创建爬虫任务 2.输入[Project Name],[Start Urls]为爬取的起始地址,可以先不输入,点击[Create]进入: 3 ...