先上代码。

 #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;
}
};

最新文章

  1. Kinect SDK 安装失败
  2. iOS - OC NSSize 尺寸
  3. java客户端连接MongoDB数据库的简单使用
  4. NOIP2000 乘积最大
  5. 关于采用MVC开发默认路由导致首页部分文件访问失效的临时解决方案
  6. insmod: can&#39;t insert &#39;led.ko&#39;: invalid module format详细解释
  7. Egret index.html设置
  8. poj 2553 强连通
  9. CentOS7快速配置nginx node mysql8.0
  10. iOS在GitHub Top 前100 简介
  11. Apex 中 PageReference 的使用
  12. OEMbutton乱码问题解决
  13. 移动基于Percona XTRADB Cluster的大数据解决方式
  14. _CSS Hack
  15. 学习python 第一章
  16. springboot-18-springboot的参数封装
  17. 简单5步,释放Mac磁盘空间
  18. entity_class实体类
  19. SSH远程登录和端口转发详解
  20. 微信 编码要UTF8

热门文章

  1. Pycharm 导入 Python 包、模块
  2. Centos 7搭建Gitlab服务器(一),搭配文章(二)一起使用,效果更好
  3. Comparison of Symbolic Deep Learning Frameworks
  4. RealProxy AOP的实现
  5. Jmeter测试计划中的元素
  6. django notes 三:Template 的查找
  7. &lt;a&gt;标签里面嵌图片&lt;img&gt;下面出现一小段空白的原因
  8. 【c++】explicit 隐式类类型转换
  9. canvas绘制经典星空连线效果
  10. [android] 天气app布局练习(三)