先上代码。

 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution {
public:
int maxArea(vector<int> &height) {
if (height.empty()) return ;
int i = , j = height.size() - ;
int max = INT_MIN;
int area;
while (i < j)
{
area = min(height[i],height[j])*(j-i);
if (area>max) max = area;
if (height[i] < height[j]) i++;//谁是短板,谁就该移动
else j--;
}
return max;
}
}; int main()
{
Solution s;
int array[] = {,,,,};
vector<int> h(array,array+);
cout << s.maxArea(h)<<endl;
return ;
}

http://blog.unieagle.net/2012/09/16/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Acontainer-with-most-water/

最笨的方法是暴力破解,加上一点预判。时间是O(n^2)

看了nandawys的评论,找到了O(n)方法,思路是从两头到中间扫描,设i,j分别指向height数组的首尾。
那么当前的area是min(height[i],height[j]) * (j-i)。
当height[i] < height[j]的时候,我们把i往后移,否则把j往前移,直到两者相遇。

这个正确性如何证明呢?
代码里面的注释说得比较清楚了,即每一步操作都能保证当前位置能取得的最大面积已经记录过了,而最开始初始化的时候最大面积记录过,所以有点类似于数学归纳法,证明这个算法是正确的。

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.

代码
600ms过大集合

class Solution {
public:
int maxArea(vector<int> &height) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int max = 0;
for( int i = 0 ; i < height.size(); ++i)
{
int hi = height[i];
if(hi == 0)
continue;
int minPosibleIndex = max / hi + i;
for(int j = height.size() - 1; j > i && j >= minPosibleIndex; --j)
{
int hj = height[j];
int area = min(hi,hj) * (j - i);
if (area > max)
max = area;
}
}
return max;
}
};

Code rewrite at 2013-1-4,O(n)

 class Solution {
public:
int maxArea(vector<int> &height) {
if (height.size() < ) return ;
int i = , j = height.size() - ;
int maxarea = ;
while(i < j) {
int area = ;
if(height[i] < height[j]) {
area = height[i] * (j-i);
//Since i is lower than j,
//so there will be no jj < j that make the area from i,jj
//is greater than area from i,j
//so the maximum area that can benefit from i is already recorded.
//thus, we move i forward.
//因为i是短板,所以如果无论j往前移动到什么位置,都不可能产生比area更大的面积
//换句话所,i能形成的最大面积已经找到了,所以可以将i向前移。
++i;
} else {
area = height[j] * (j-i);
//the same reason as above
//同理
--j;
}
if(maxarea < area) maxarea = area;
}
return maxarea;
}
};

LeetCode-Container With Most Water-zz的更多相关文章

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

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

  2. [LeetCode] Container With Most Water 装最多水的容器

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

  3. [LeetCode]Container With Most Water, 解题报告

    前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...

  4. C++LeetCode:: Container With Most Water

    本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...

  5. leetcode Container With Most Water

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

  6. [LeetCode] Container With Most Water 简要分析

    前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...

  7. [Leetcode] Container With Most Water ( C++)

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

  8. LeetCode——Container With Most Water

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

  9. LeetCode Container With Most Water (Two Pointers)

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

  10. [Leetcode] container with most water 最大水容器

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

随机推荐

  1. Python 实现flatten功能

    from collections import Iterable def flatten(items): for x in items: if isinstance(x, Iterable) and ...

  2. vue相关问题在工作中的问题及ui组件及html样式搭建相关网站下载资源

    https://youzan.github.io/vant/#/zh-CN/nav-bar http://www.builive.com/docs/api/index.html    bui框架BUI ...

  3. dfs.replication、dfs.replication.min/max及dfs.safemode.threshold.pct

    一.参数含义 dfs.replication:设置数据块应该被复制的份数: dfs.replication.min:所规定的数据块副本的最小份数: dfs.replication.max:所规定的数据 ...

  4. Visual Studio 2013复制项目

    在当前解决方案下复制项目的步骤: 1. 在硬盘存放代码的目录下将整个文件拷贝一份,修改文件夹名字,改成新的项目名称, 然后修改 *.csproj文件,名字必须与新项目名一致. 2. 在解决方案上右键 ...

  5. poj 2259 Team Queue

    Team Queue Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2977   Accepted: 1092 Descri ...

  6. [springBoot系列]--spring-boot-devtools在Idea中热部署方法

    1 pom.xml文件 注:热部署功能spring-boot-1.3开始有的 <!--添加依赖--> <dependency> <groupId>org.sprin ...

  7. (原创).Net将EF运用于Oralce一 准备工作

    网上有很多EF运用于Oracle的博文,但是找了半天发现大多数博文大都语焉不详,于是决定自己折腾. 首先我的开发工具为vs2010,那么最适用于VS2010的EF版本为多少呢?答案是EF5.我在Sta ...

  8. C# 页面抽奖实例 asp.net

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...

  9. Python基础学习总结(五)

    7.用户输入输出和while循环 1.使用函数 input() 输入,print() 打印,字符串可以用逗号隔开.end=' ' 关键字参数,打印时可以不换行,sep=‘ 你想要的分隔符 ’,关键字参 ...

  10. Hadoop 完全分布式部署(三节点)

    用来测试,我在VMware下用Centos7搭起一个三节点的Hadoop完全分布式集群.其中NameNode和DataNode在同一台机器上,如果有条件建议大家把NameNode单独放在一台机器上,因 ...