LeetCode-Container With Most Water-zz
先上代码。
#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的更多相关文章
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode]Container With Most Water, 解题报告
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...
- C++LeetCode:: Container With Most Water
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...
- leetcode Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] Container With Most Water 简要分析
前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...
- [Leetcode] Container With Most Water ( C++)
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- LeetCode——Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- LeetCode Container With Most Water (Two Pointers)
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- [Leetcode] container with most water 最大水容器
Given n non-negative integers a1 , a2 , ..., an , where each represents a point at coordinate (i, ai ...
随机推荐
- python学习14-模块
引入模块的方式: 1. import 模块 2. from xxx import 模块 一.collections 模块 1.Counter() counter是一个计数器,主要用来计数,计算一个字符 ...
- MySQL按照月进行统计
MySQL按照月进行统计 今天需要后台提供一个按月统计的API.所以查了一下SQL语句的实现方法. 按月统计SQL select date_format(createtime, '%Y-%m') as ...
- jquery jgrid filterToolBar beforeSearch 修改postData
beforeSearch: function() { var posted_data = $("#mygrid").jqGrid('getGridParam,'postData') ...
- 九度oj 1002 Grading 2011年浙江大学计算机及软件工程研究生机试真题
#include<iostream> #include<queue> #include<cstdio> #include<cstring> #inclu ...
- CentOS 7下使用yum安装MySQL5.7
1.卸载 1.1先停掉mysql进程,没有安装过的可以直接跳过 pkill - mysqld rpm -qa|grep -i mysql 1.2用命令 yum -y remove -.el7.x86_ ...
- 封装常用的跨浏览器的事件对象EventUtil
var EventUtil = { addHandler: function(element,type,handler) { //事件监听 if(element.addEventListener) { ...
- ODBC, OLEDB, ADO, ADO.NET
在SSIS中,选择Connction Manager时,有很多的类型.其中会发现如下4个连接类型ODBC, OLEDB, ADO, ADO.NET.这4个东东到底是什么,他们有什么关联,什么区别,如何 ...
- C# xml操作word-->word转2003xml
1.第一步,准备word模版
- javascript下的arguments,caller,callee,call,apply示例及理解
(参考:http://justcoding.iteye.com/blog/589111) Arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments ...
- 在 Web 应用中创建 Node.js 应用程序
本分步指南将通过 Azure Web 应用帮助您启动并运行示例 Node.JS 应用程序.除 Node.JS 外,Azure Web 应用还支持其他语言,如 PHP..NET.Node.JS.Pyth ...