题意:

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. QueryInterface

    QueryInterface IUnknown *p2; hr = pInnerUnknown->QueryInterface(vGUID2, (void**)&p2); IUnknow ...

  2. Spark生态系统BDAS

    目前,Spark已经发展成为包含众多子项目的大数据计算平台. 伯克利将Spark的整个生态系统称为伯克利数据分析栈(BDAS). 其核心框架是Spark,同时BDAS涵盖支持结构化数据SQL查询与分析 ...

  3. Docker 入门教程(转)

    add by zhj: 可以简单的认为docker是对LXC(Linux Container)封装,它提供一种比LXC高级的API.Docker使用Go语言开发,利用了Linux提供的LXC,AUFS ...

  4. Sharding & IDs at Instagram(转)

    英文原文:http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram 译文:http://ww ...

  5. JavaScript 核心参考教程 内置对象

    这个标准基于 JavaScript (Netscape) 和 JScript (Microsoft).Netscape (Navigator 2.0) 的 Brendan Eich 发明了这门语言,从 ...

  6. if condition volist

    <table class="table table-hover table-striped"> <if condition="$order_list e ...

  7. 更有效率的使用Visual Studio(一)

    很多比较通用的快捷键的默认设置其实是有一些缩写在里面的,这个估计也是MS帮助我们记忆.比如说注释代码的快捷键是Ctrl + E + C,我们如果知道它是 Ctrl + Edit + Comment C ...

  8. hdoj 5387(Clock)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5387 比较水的一道题目,也是自己单翘的第一道题目吧,题意就是找到给定时间时钟三个指针之间的夹角, 需要 ...

  9. JS:公历、农历互转

    先申明这段代码不是我写的,纯粹只是觉的比较好用,所以记录下来以后继续使用,也同样分享给大家,大家有更好的可以推荐给我,谢谢! function CalConv(M, dateStr) { if (da ...

  10. jquery提示信息 tips

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...