[Leetcode] Container With Most Water ( C++)
题目:
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.
Tag:
Array; Two Pointers
体会:
1. 这道题我觉得是双指针的一个创新用法。之前的双指针都是一主一辅,辅助的那个不断去探测下一个,然后主要的那个会根据探测结果做出相应动作,比如 Merge Sorted Array,这道题的双指针两个人是同等重要性,根据其他的判定条件来决定下一次移动谁。
2. 这道题之所以是双指针是同等位置,也可以从另外一个角度来感受,就是要知道左边那条线“和”右边那条线的位置,两个都是未知的,都是要确定的。
3. 回到题目上,O(N)的解法。代码很简单,可是能想到不容易。(我也是炒的别人的思路)。为什么每次是那样移动指针呢?假设h[left] < h[right],我们管当前用来围城面积的左边的这条竖着的直线叫Line left, 管右边的那条线要Line right。那么当前的面积就是 (area = (right - left) * h[left])。好了,我们假设还有一个更大的面积,下面我们要说明这个更大的面积一定不会是和Line left围成的。
假设这个更大的面积的其中一条边是Line x, (Line x肯定是位于Line left 和Line right之间啦)。那么Line和max围成的面积是( ( x - left) * h[left]) , 这个面积肯定是小于原来的 (right - left ) * h[left]的啦。
综上,我们肯定是要移动Line left了。
4. 可以从一个更数学的角度来解释这个问题:
假设left, right 是line left和line right在x轴上的坐标,起始时, left = 0, right = 最右边。那么
area = ( right - left ) * min (h[left], h[right])
怎么样才能使这个函数有更大的值呢?不管移动左边的线还是右边的线,结果都是(right - left)的值会变小,若要area的值变大,只能是把min(h[left], h[right])的值变大才有可能。所以如果我们继续保留较矮的那条线的话,这个min值一定不会变大,所以面积肯定不会变大;而保留原本较高的那条线,是有可能使min值变大的,所以只能是保留这条线。
class Solution {
public:
int maxArea(vector<int> &height) {
int left = ;
int right = height.size() - ;
int result = ;
int area = ;
while (left < right) {
if (height[left] < height[right]) {
area = (right - left) * height[left++];
} else {
area = (right - left) * height[right--];
}
if (area > result) {
result = area;
}
}
return result;
}
};
[Leetcode] Container With Most Water ( C++)的更多相关文章
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- [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 ...
- 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). ...
- LeetCode Container With Most Water (Two Pointers)
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- [Leetcode] container with most water 最大水容器
Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ...
随机推荐
- 关于毕设WiFi选型
毕设做基于WiFi的家庭灯光影音控制,现在主要是WiFi模块上的选型,自己选了ESP8266. 一开始是看到了机智云的内容,想尝试机智云.但是目前没有母版,不知道怎么下手. 接下来就先尝试通过通用类型 ...
- 【Nutch2.2.1基础教程之1】nutch相关异常
1.在任务一开始运行,注入Url时即出现以下错误. InjectorJob: Injecting urlDir: urls InjectorJob: Using class org.apache.go ...
- window.onload,<body onload="function()">, document.onreadystatechange, httpRequest.onreadystatechang
部分内容参考:http://www.aspbc.com/tech/showtech.asp?id=1256 在开发的过程中,经常使用window.onload和body onload两种,很少使用do ...
- 模拟Sping MVC
在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...
- U盘启动时无USB-HDD选项的解决方案
今天在使用一块老板子的时候 发现没有USB-HDD启动项 在启动顺序中只有 USB-ZIP(ZIP) -FDD(软盘) -CDROM(光驱) 1.插入U盘 2.开机 3.在BIOS中找到Hard D ...
- 【转】sqlmap用户手册
http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 当给sqlmap这么一个url的时候,它会: 1.判断可注入的参数2.判断可以用那种SQL注入 ...
- [原]Sublime Text3 搭建16位汇编环境(windows)
最近在学习王爽的<汇编程序>,参考<简单OS开发前奏<一>EDITPLUS+MASM32搭建汇编开发环境(16位+32位)>http://www.cnblogs.c ...
- 《Programming WPF》翻译 第3章 3.内嵌控件
原文:<Programming WPF>翻译 第3章 3.内嵌控件 WPF提供了一系列内嵌控件.其中大多数符合标准的你已经熟悉的Windows控件类型.注意到没有一个是包装在旧的Win32 ...
- logstash date插件介绍
时间处理(Date) 之前章节已经提过, filters/date 插件可以用来转换你的日志记录中的时间字符串,变成 LogStash::Timestamp 对象,然后转存到 @timestamp 字 ...
- Windows通用应用平台
什么是 UWP? 很多程序员都有一个梦想:希望自己开发的软件能够轻而易举的在所有平台上运行,而不是把同样的需求,用不同的技术.工具重新开发才能够运行在所有平台上.这就是跨平台,很多软件从业者都在为这个 ...