LeetCode(11) ContainerWithMostWater
来源:互联网 发布:网站 ip地址 域名绑定 编辑:程序博客网 时间:2024/05/22 10:23
题目如下:
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.
一开始,最自然地想法是从左到右把数组遍历一下,暴力地使用了2层循环,时间复杂度为O(N²)提交了发现超时了。。。。
class Solution {public: int maxArea(vector<int> &height) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int area_max=0; int area_tmp=0; int height_min=0; int i=0; int j=(int)height.size()-1; while(i<j) { height_min=height[i]<height[j]?height[i]:height[j]; area_tmp = (j-i)*(height[i]<height[j]?height[i]:height[j]); if(area_tmp>area_max) area_max=area_tmp; if(height[j]>height[i]) { while(height[i]<=height_min) { i++; } } else { while(height[j]<=height_min) { j--; } } // std::cout<<"area_tmp="<<area_tmp<<", i="<<i<<" ,j="<<j<<", area_max="<<area_max<<std::endl; } return area_max; }};
然后猜测,难道本题需要用动态规划,但是发现根本没法写状态转移方程。实在找不到办法了,却发现网上的解答中有非常简洁的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通过。
class Solution {public: int maxArea(vector<int> &height) { int area_max=0; int area_tmp=0; int i=0; int j=(int)height.size()-1; while(i<j) { area_tmp = (j-i)*(height[i]<height[j]?height[i]:height[j]); if(area_tmp>area_max) area_max=area_tmp; if(height[j]>height[i]) i++; else j--; } return area_max; }};
然后再考虑一下有没有优化的空间呢?
唯一能够想到是这里
if(height[j]>height[i]) i++; else j--;
这里每次把变量i或者j移动1步。如果新的高度比老的高度还小,那显然还要继续移动,所以这里可以修改为每次移动多步。
if(height[j]>height[i]) { while((height[i]<=height_min)&&(i<j)){ i++; } } else { while((height[j]<=height_min)&&(i<j)) { j--; } }
小结
(1) 从两边向中间移动是个不错的办法。仔细想想也符合这道题算最大面积的风格。
(2)每次更新下标的时候,到底更新i还是更新j呢?这道题挺有意思的地方在于,更新的标准不是通常所理解的i或者j哪个一定更好就更新哪个,而是哪个可能更好就更新哪个,或者说,如果更新i一定会更差,那么就更新j看看不会不变好。
- LeetCode(11) ContainerWithMostWater
- 11ContainerWithMostWater
- leetcode11.ContainerWithMostWater
- 【LeetCode-面试算法经典-Java实现】【011-ContainerWithMostWater(容纳最多的水)】
- leetcode 11
- leetcode 11
- LeetCode 11
- Leetcode(11)
- leetcode 11
- leetcode 11
- leetCode 11
- [LeetCode] Palindrome Partition [11]
- LeetCode之旅(11)
- [Leetcode]11 Rotate Array
- leetcode第11题
- leetcode 11 in c++
- leetCode练习(11)
- LeetCode OJ(11)
- 从32位程序开发进入64位程序开发
- WINDOWS_32位与64位系统的编程差异
- 64位与32位编程的数据类型区别
- android sqllite 支持的数据类型
- Java 集合(Collections)总结
- LeetCode(11) ContainerWithMostWater
- PHP运行出现Notice : Use of undefined constant 的解决办法
- CRM上线之路 走上了CRM实施顾问-第88天上班 -第19周
- AJAX_1
- 正则获取key value
- C语言控制台小游戏
- 帐号状态account_status
- delphi7的调试功能
- java web移植 遇到Project facet Java version 1.7 is not supported