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 ...
随机推荐
- linux du命令: 显示文件、目录大小
介绍:du命令用于显示指定文件(夹)在磁盘中所占的空间信息.假如指定的文件参数实际上是一个目录,就要计算该目录下的所有文件.假如 没有提供文件参数,执行du命令,显示当前目录内的文件占用空间信息. 语 ...
- easyui问题小记
在easyui1.4.3版本中,(其他版本不知道是不是也是这样的),绑定在datagridview上面的数据最好不要是带有特殊符号的字段,不然会导致部分的属性不好用,比如这样的字段 START_DA ...
- win2008 64位 + oracle11G 64位 IIS7.5 配置WEBSERVICE
第一个错误: 安装过程依旧是那样简单,但在配好IIS站点,准备连接数据库的时候出错了,以下是错误提示:System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更 ...
- jgroups 入门
官网地址:http://www.jgroups.org/ 聊天室示例:http://www.jgroups.org/tutorial/html/ch02.html 2.1. JGroups overv ...
- arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
测试方法 function test(){ var arr = [0,1,2,3]; arr.splice(1,1,'a');//case console.dir(arr); } case1: arr ...
- CGI 是什么
CGI是公共网关接口,是Java Servlet 的前身,Java Servlet 是运行在服务器端的小程序.
- Uncaught TypeError: Object #<Object> has no method 'fancybox'
Uncaught TypeError: Object #<Object> has no method 'fancybox' 2011-10-24 16:51:19| 分类: html|举 ...
- Java读取Properties文件的六种方法
使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法示例: InputStream in = lnew BufferedIn ...
- 网页布局:float与position的区别
网页开发中布局是一个永恒的话题.巧妙的布局会让网页具有良好的适应性和扩展性.css的布局主要涉及两个属性——position和float.它们俩看上去很容易被弄混,可是仔细分析一下,它们的区别还是很明 ...
- Castle IOC容器内幕故事(上)
主要内容 1.WindsorContainer分析 2.MicroKernel分析 3.注册组件流程 一.WindsorContainer分析 WindsorContainer是Castle的IOC容 ...