LeetCode11 Container With Most Water
题意:
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.
分析:
自己做的时候脑子并不是很清楚(帮老板画图画的...),不过一步一步优化也算AC了。但是看着跑的时间那么高就知道肯定不是最优,
学习讨论区后知道了O(n)算法的思路并实现,思考历程记录如下:
1.暴力先写一个,把所有区间来一遍,O(n^2)。当然肯定超时...代码还是记录一下吧
class Solution {
public:
int maxArea(vector<int>& height) {
int result = ;
for (int i = ; i < height.size(); ++i) {
for (int j = ; j < i; ++j) {
int h = i - j;
int l = min (height[i], height[j]);
result = max(result, h * l);
}
}
return result;
}
};
2. 不按照区间走,按照不同高度,高度依次向上时,找到符合该高度的最长区间(两头扫),然后比较。
高度用个map存,代码如下:(分析时间复杂度O(m*n*logm) m不同高度的数量),诸如1,2,3...15000全是不同高度数字的样例也会超时
class Solution {
public:
int maxArea(vector<int>& height) {
set<int> s;
for (int i = ; i < height.size(); ++i) {
s.insert(height[i]);
}
auto itr = s.begin();
int result = (*itr) * (height.size() - );
for ( itr = s.begin(); itr != s.end(); ++itr) {
int left = , right = height.size() - ;
while (height[left] < (*itr) ) {
++left;
}
while (height[right] < (*itr) ) {
--right;
}
result = max(result, (right - left) * (*itr) );
}
return result;
}
};
3.分析上述代码,其实left,right这里根本没必要每次都从头遍历。因为height是递增的,所以left,right在上一次基础上继续走即可。
所以代码内层只需一遍遍历,复杂度O(m*logm),这个可以AC了。
class Solution {
public:
int maxArea(vector<int>& height) {
set<int> s;
for (int i = ; i < height.size(); ++i) {
s.insert(height[i]);
}
auto itr = s.begin();
int result = ;
int left = , right = height.size() - ; //这句优化!
for (itr = s.begin(); itr != s.end(); ++itr) {
while (height[left] < (*itr) ) {
++left;
}
while (height[right] < (*itr) ) {
--right;
}
result = max(result, (right - left) * (*itr) );
}
return result;
}
};
4.实际上,也没有必要按照高度存储和遍历。
注意到如果从最大长度区间开始向内遍历,只有当高度更高时才有可能更新面积(因为长度已经比之前小),所以,两根指针一遍遍历即可。O(n)
代码:
class Solution {
public:
int maxArea(vector<int>& height) {
int i = , j = height.size() - , result = ;
while (i < j) {
int minHeight = min(height[i], height[j]);
result = max(result, minHeight * (j - i));
while (height[i] <= minHeight) {
i++;
}
while (height[j] <= minHeight) {
j--;
}
}
return result;
}
};
LeetCode11 Container With Most Water的更多相关文章
- Leetcode11 Container With Most Water 解题思路 (Python)
今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...
- 思维题(两点逼近)LeetCode11 Container with Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- Leetcode11.Container With Most Water盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- [array] leetCode-11. Container With Most Water-Medium
leetCode-11. Container With Most Water-Medium descrition Given n non-negative integers a1, a2, ..., ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- [LintCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 67. Container With Most Water
Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- No.011 Container With Most Water
11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...
随机推荐
- 第三百五十七天 how can I 坚持
502是我对你没有爱的意思吗?为什么要要这样啊,好绝情. 明天要去加班,今晚回来也好晚了,晚上回来都有点精神恍惚了. 他们要聚会,本来要想去樱木花道来,哎. 后天..什么都没学. .. .. .. 准 ...
- 老 base64 for xe8
not recommend ,only for study procedure TForm1.Button3Click(Sender: TObject); var ssi, sso: TStringS ...
- Windows Azure使用必读(转)
原文:http://www.cnblogs.com/dyllove98/archive/2013/06/15/3137528.html 近些日子帮了不少用户移植应用到了Windows Azure上,在 ...
- <系统函数实现>memcmp
这是我实现的memcmp函数: #include <stdio.h> #include <string.h> /* *int memcmp (const void *s1,co ...
- vim之pydiction插件
[vim之pydiction插件] It consists of three main files: python_pydiction.vim -- Vim plugin. complete-dict ...
- flask中的session对象方法
'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys ...
- Array types are now written with the brackets around the element type
因为网站翻译的时候应该用的beta/beta2,而再beta4中就会出现问题,解决问题方案: var shopping: String[] = ["Eggs","Milk ...
- RPG JS:免费开源的跨平台RPG游戏引擎
RPG JS是一个2D RPG游戏制作引擎,目前版本基于Ease|JS游戏引擎,基于Canvas Engine的新版本即将发布. RPG JS是免费且开源的. RPG JS有着完善的文档支持. RPG ...
- Android - 应用名称设置的问题
今天我想修改我的android应用名称,就是手机桌面上图标下面的名称,根据我的理解我修改AndroidManifest.xml文件中application标签中的android:label=" ...
- 编译安装-MySQL5.5
一.参数选项 1.目录选项 2.存储引擎选项 3.库文件加载选项 二.安装 1.环境准备 2.安装前的系统设置 3.安装执行 4.初始化数据库 5.注册为服务 6.加入环境变量 7.启动服务 8.重新 ...