题意:

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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的更多相关文章

  1. Leetcode11 Container With Most Water 解题思路 (Python)

    今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...

  2. 思维题(两点逼近)LeetCode11 Container with Most Water

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

  3. Leetcode11.Container With Most Water盛最多水的容器

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

  4. [array] leetCode-11. Container With Most Water-Medium

    leetCode-11. Container With Most Water-Medium descrition Given n non-negative integers a1, a2, ..., ...

  5. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  6. [LintCode] Container With Most Water 装最多水的容器

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

  7. 67. Container With Most Water

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

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

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

  9. No.011 Container With Most Water

    11. Container With Most Water Total Accepted: 86363 Total Submissions: 244589 Difficulty: Medium Giv ...

随机推荐

  1. [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序

    一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 ...

  2. 使用DNSPod来处理网站的均衡负载(转)

    add by zhj:配置倒是蛮简单的,其实就是把域名与多个IP进行关联,在数据库中实现这个应该也是蛮简单的. 原文:http://kb.cnblogs.com/page/75571/ 首先介绍下DN ...

  3. POJ1275Cashier Employment(查分约束系统)

    链接1275Cashier Employment 题目大意就是说有一些人来应聘一个超级市场的工作,每个人的应聘的起始时间在0~23时之间,而超市在时间i需要R[i]个工作人员,而每个人的工作时间都是8 ...

  4. 编译安装-PHP

    一.编译配置选项2 配置帮助表:2 安装目录:2 交叉编译选项:2 特征选项:3 SAPI模块设置:3 普通参数设置:4 扩展参数:4 PEAR相关选项:9 ZEND相关选项:9 TSRM线程安全资源 ...

  5. 操作Cookie的一个陷阱服务器端获取不了maxAge或其它属性

    搞了几天终于弄明白了这个问题: 在读取Cookie,然后操作时,除了getName(),getValue()外,不要妄图得到其他信息,如下方法不会得到值的:cookie.getMaxAge();=== ...

  6. SSH下载的方法

    ----------------------------------下作下载方法一----------------------------------------------------------- ...

  7. JAVA自定义注释(Target,Retention,Documented,Inherit)

    java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能.注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用.包含在 java.l ...

  8. Objc基础学习记录--UIViewController

    多个view之间切换用Embed in -->UINavigationController sugues的方式  有push和modal 及其cuestom 关于其重要的方法: -(void) ...

  9. xiaocms 关于搜索功能 添加搜索字段

    自己折磨了好几天 就是没研究个出像样的的东西 看了一下 core/controller/index.php searchAction()方法 但是不知从何下手.查了sql语句,还是没实现 请教了一位自 ...

  10. 在js中使用createElement创建HTML对象和元素

    1.创建链接 <script language="javascript"> var o = document.body; //创建链接 function createA ...