题意:

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. (原创)jquery插件-可选可填控件

    ;(function ($) { $.fn.autoFillTextBox = function (options) { var defaults = { nShow: 5, //显示条数 nLen: ...

  2. JavaScript 继承方式详解

    js继承的概念 js里常用的如下两种继承方式: 原型链继承(对象间的继承)类式继承(构造函数间的继承) 由于js不像java那样是真正面向对象的语言,js是基于对象的,它没有类的概念.所以,要想实现继 ...

  3. HTML5每日一练之视频标签的应用

    与音频一样,在过去,我们如果想在Web上播放视频,也是都是通过Flash来播放,同样并不是所有的浏览器都安装了Flash播放器插件,而现在我们在HTML5中,就能完全脱离Flash或其他的插件来播放视 ...

  4. MYSQL数据库性能调优之八:mysql日志

    MySQL日志 主要包含:错误日志.查询日志.慢查询日志.事务日志.二进制日志.中继日志: 使用 SHOW GLOBAL VARIABLES LIKE '%log%';  查询所有日志配置详情: 一. ...

  5. java线性表学习笔记(一)

    线性表是一种按顺序储存数据是的常用结构,大多数的线性表都支持以下的典型操作: 从线性表提取插入删除一个数据: 找出线性表中的某一个元素: 找出线性表中的元素: 确定线性表中是否包含某一个元素,确定线性 ...

  6. iOS 8自动调整UITableView和UICollectionView布局

    本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...

  7. oracle根据正则表达式查找对应的字段

    语法如下: SELECT * FROM 表名WHERE regexp_like(表字段,'正则') 例如: 查找某字段小数点后有两个小数以上的信息 SELECT * FROM A TWHERE reg ...

  8. POJ3080Blue Jeans(暴力)

    开始做字符串专题,地址 第一题水题,暴力就可以做 #include <map> #include <set> #include <stack> #include & ...

  9. 如何在C#中模拟C++的联合(Union)?[C#, C++] How To Simulate C++ Union In C#?

    1 什么是联合? 联合(Union)是一种特殊的类,一个联合中的数据成员在内存中的存储是互相重叠的.每个数据成员都在相同的内存地址开始.分配给联合的存储区数量是“要包含它最大的数据成员”所需的内存数. ...

  10. 项目视频讲解_[HeyJava][尚学堂][CMS文章内容管理系统]

    [HeyJava][尚学堂][CMS文章内容管理系统] http://pan.baidu.com/s/1c0imHrE