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 ...
随机推荐
- UUID生成工具
public class UUIDUtils { private static SecureRandom SEEDER_STATIC = null; private static byte[] ADD ...
- 小y的质数
题目链接:https://ac.nowcoder.com/acm/contest/634/C 链接:https://ac.nowcoder.com/acm/contest/634/C来源:牛客网 题目 ...
- python学习-基础知识-1
1.计算机历史 计算机使用高低电压的两种状态来描述信息.计算机可以理解的只有二进制数据即010100011....,1个比特位可以表示的状态只有2种,n个比特位可以表示的状态有2的n次方种. 所以如果 ...
- jsoup、xpath教程
一.jsoup 1.使用JSOUP处理HTML文档 2.使用 jsoup 对 HTML 文档进行解析和操作 3.jsoup开发指南,jsoup中文使用手册,jsoup中文文档 二.xpath 1.XP ...
- 记一次走心One 2 One沟通
聊的比较零零碎碎,内容比较散,有些solution不错,记一些要点吧(1)要学会汇报,就是坐你身边的人,也未必知道你在干啥 三个人都在砌墙.当人们分别问他们在做什么,他们的答案却不一样:第一个人头也没 ...
- js 递归树结构数据查找父级
1.json树数据查找所有父级--完成 json:树结构数据 var arrData = [{ "label": "中国", "City": ...
- Java - 生成keystore
有个需求,说要在生成PDF文件时加上signature.操作PDF容易,用: <dependency> <groupId>com.itextpdf</groupId> ...
- Be opinionated out of the box but get out of the way quickly as requirements start to diverge from
Be opinionated out of the box but get out of the way quickly as requirements start to diverge from t ...
- hdu 1712 (分组背包)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17676 这个问题让我对分组背包更清晰了一点,主要是这个问题: 使用一维数组的 ...
- 【原创】MapReduce程序如何在集群上执行
首先了解下资源调度管理框架Yarn. Yarn的结构(如图): Resource Manager (rm)负责调度管理整个集群上的资源,而每一个计算节点上都会有一个Node Manager(nm)来负 ...