leetcode 11. Container With Most Water
2016-03-23 18:29
429 查看
传送门
Question
Total Accepted: 72326 Total Submissions: 211394 Difficulty: Medium
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.
Subscribe to see which companies asked this question
Hide Tags
Array Two Pointers
Show Similar Problems
题意以及题解转自:
/article/1344157.html
很不错的贪心题,赞
然后猜测,难道本题需要用动态规划,但是发现根本没法写状态转移方程。实在找不到办法了,却发现网上的解答中有非常简洁的O(N)时间复杂度的解答,应该说,算是一种贪心策略。
两个下标变量,分别表示数组的头部和尾部,逐步向中心推移。这个推移的过程是这样的:
假设现在是初始状态,下标变量i=0表示头部,下标变量j=height.size(),表示尾部,那么显然此时的容器的装水量取决一个矩形的大小,这个矩形的长度为j-i,高度为height[i]与height[j]的最小值(假设height[i]小于height[j])。接下来考虑是把头部下标i向右移动还是把尾部下标j向左移动呢?如果移动尾部变量j,那么就算height[j]变高了,装水量依然没有变得更大,因为短板在头部变量i。所以应该移动头部变量i。也就是说,每次移动头部变量i和尾部变量j中的一个,哪个变量的高度小,就把这个变量向中心移动。计算此时的装水量并且和最大装水量的当前值做比较。
解答如下,在LeetCode的OJ上用100ms通过。
小结
(1) 从两边向中间移动是个不错的办法。仔细想想也符合这道题算最大面积的风格。
(2)每次更新下标的时候,到底更新i还是更新j呢?这道题挺有意思的地方在于,更新的标准不是通常所理解的i或者j哪个一定更好就更新哪个,而是哪个可能更好就更新哪个,或者说,如果更新i一定会更差,那么就更新j看看不会不变好。
代码:
11. Container With Most Water
My SubmissionsQuestion
Total Accepted: 72326 Total Submissions: 211394 Difficulty: Medium
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.
Subscribe to see which companies asked this question
Hide Tags
Array Two Pointers
Show Similar Problems
题意以及题解转自:
/article/1344157.html
很不错的贪心题,赞
然后猜测,难道本题需要用动态规划,但是发现根本没法写状态转移方程。实在找不到办法了,却发现网上的解答中有非常简洁的O(N)时间复杂度的解答,应该说,算是一种贪心策略。
两个下标变量,分别表示数组的头部和尾部,逐步向中心推移。这个推移的过程是这样的:
假设现在是初始状态,下标变量i=0表示头部,下标变量j=height.size(),表示尾部,那么显然此时的容器的装水量取决一个矩形的大小,这个矩形的长度为j-i,高度为height[i]与height[j]的最小值(假设height[i]小于height[j])。接下来考虑是把头部下标i向右移动还是把尾部下标j向左移动呢?如果移动尾部变量j,那么就算height[j]变高了,装水量依然没有变得更大,因为短板在头部变量i。所以应该移动头部变量i。也就是说,每次移动头部变量i和尾部变量j中的一个,哪个变量的高度小,就把这个变量向中心移动。计算此时的装水量并且和最大装水量的当前值做比较。
解答如下,在LeetCode的OJ上用100ms通过。
小结
(1) 从两边向中间移动是个不错的办法。仔细想想也符合这道题算最大面积的风格。
(2)每次更新下标的时候,到底更新i还是更新j呢?这道题挺有意思的地方在于,更新的标准不是通常所理解的i或者j哪个一定更好就更新哪个,而是哪个可能更好就更新哪个,或者说,如果更新i一定会更差,那么就更新j看看不会不变好。
代码:
class Solution { public: int maxArea(vector<int>& height) { int ma = 0; int te = 0; int i,j; int le; i = 0;j = height.size() - 1; le = j; while(i < j){ if(height[i] <= height[j]){ te = le * height[i]; ma = max(ma,te); i++; } else{ te = le * height[j]; ma = max(ma,te); j--; } le --; } return ma; } };
相关文章推荐
- HDU 2457:DNA repair
- 杭电1787GCD Again(欧拉函数)
- [Leetcode 265]Paint House II
- [Leetcode 256]Paint House
- [AI]根据葡萄特征聚类
- [AI](加入随机因素)局部搜索解决N皇后问题(含源码)
- BN: Accelerating Deep Network Training by Reducing Internal Covariate Shift
- IBM openblockchain学习(二)--chaincode源码分析
- public static void main(String args[]){ }中的String args[ ]
- Convention 插件学习
- PAT 1014 Waiting in Line (30)
- 海外投行抢滩大数据 人工智能等领域趋热
- python的smtplib发送email给多人要传列表而不是','.join(LIST)
- QDialog QFrame QWidget QMainWindow区别
- wait与waitpid
- 【技术向】rainmeter的设计与发现
- shiro配置filterchaindefinitions实现多角色判定方法的重写
- Invalid file name: must contain only [a-z0-9_.]
- Groovy DSL (1) Command chains
- pair 函数 (STL)