LeetCode | Container with most water(装最多的水)
来源:互联网 发布:java redis 高级应用 编辑:程序博客网 时间:2024/05/30 20:07
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.
题目就是说,在i位置有高度为ai的线,那么从a1到an任选两条线,选出装入最多水的面积。
思路一:
一个很简单的方法,就是任选两个边,然后取其最小值min,再乘以距离(j-i),就求得面积。取最大值即可。
代码如下:
int maxArea(vector<int> &height) { // Start typing your C/C++ solution below // DO NOT write int main() function int size = height.size(); int max = 0;for(int i = 0; i < size; ++i){for(int j =i+1; j < size; ++j){int min = height[i] < height[j] ? height[i] : height[j];int tmp = min*(j-i);if(tmp> max) max = tmp;}}return max; }
这个方法简单,但是时间复杂度太高,需要优化。
思路二:
我们先选择最两头的边,求得面积,然后让较小的边向内移动,如果移动较大边的话,很可能把最大值的边给移去了,得不到所求解。但移动最小值的话,希望找到大一点的边再求值。
int MostWater(int arr[],int n){ int lo = 0; int hi = n-1; int area = 0; int temparea; while(lo < hi){ int min = arr[lo] < arr[hi] ? arr[lo] : arr[hi]; temparea = min * (hi - lo); if(temparea > area) area = temparea; if(arr[lo] < arr[hi]) lo++; else hi--; } return area;}
思路三:
下面以例子: [4,6,2,6,7,11,2] 来讲解。
1.首先假设我们找到能取最大容积的纵线为 i , j (假定i<j),那么得到的最大容积 C = min( ai , aj ) * ( j- i) ;
2.下面我们看这么一条性质:
①: 在 j 的右端没有一条线会比它高! 假设存在 k |( j<k && ak > aj) ,那么 由 ak> aj,所以 min( ai,aj, ak) =min(ai,aj) ,所以由i, k构成的容器的容积C' = min(ai,aj ) * ( k-i) > C,与C是最值矛盾,所以得证j的后边不会有比它还高的线;
②:同理,在i的左边也不会有比它高的线;
这说明什么呢?如果我们目前得到的候选: 设为 x, y两条线(x< y),那么能够得到比它更大容积的新的两条边必然在 [x,y]区间内并且 ax' > =ax , ay'>= ay;
3.所以我们从两头向中间靠拢,同时更新候选值;在收缩区间的时候优先从 x, y中较小的边开始收缩;
直观的解释是:容积即面积,它受长和高的影响,当长度减小时候,高必须增长才有可能提升面积,所以我们从长度最长时开始递减,然后寻找更高的线来更新候补;
因此代码如下:
int MostWater_1(int arr[],int n){ int lo = 0,hi = n-1; int area = 0; int temparea; while(lo < hi){ int min = arr[lo] < arr[hi] ? arr[lo] : arr[hi]; temparea = min * (hi - lo); if(temparea > area) area = temparea; if(arr[lo] < arr[hi]){ int templo = lo+1; while(templo < hi && arr[templo] <= arr[lo]) ++templo; lo = templo; }else{ int temphi = hi-1; while(temphi > lo && arr[temphi] <= arr[hi]) temphi--; hi = temphi; } } return area;}
- LeetCode | Container with most water(装最多的水)
- LeetCode OJ 之 Container With Most Water(能装最多水的容器)
- Leetcode刷题记——11. Container With Most Water(装最多水的容器)
- Container With Most Water 装最多的水
- Container With Most Water(能装最多水的容器)
- LeetCode 11. Container With Most Water(盛最多的水)
- Container With Most Water --装最多水的容器(重)
- 383.Container With Most Water-装最多水的容器(中等题)
- 383.Container With Most Water-装最多水的容器(中等题)
- 6.container-with-most-water(装最多水的容器)
- Container With Most Water 盛最多的水
- Container With Most Water (容器中盛最多的水)
- 含水最多的容器(Container With Most Water)
- Container With Most Water 求容器里能放最多的水 @LeetCode
- LeetCode 11. Container With Most Water(容器装水)
- 11. Container With Most Water(求能装最多水的容器)
- [LeetCode]Container With Most Water(贪心)
- LeetCode(11) Container With Most Water
- linux scp有证书的传输
- 一个数独算法,以及对于这段代码的我之见解。
- Spring注解@Component、@Repository、@Service、@Controller区别
- vi/vim显示中文字符并且去掉^M的方法
- 55454g4hjgjgj
- LeetCode | Container with most water(装最多的水)
- hibernate.cfg.xml问题
- 2、jQuery对象与Dom对象的区别以及转换
- Java HashMap 两种遍历方法性能比较
- 3、jQuery基础重点之链式语法
- PoolableObjectFactory和ObjectPool
- 这才是9天事情然后的游芳
- 4、jQuery基础重点事件
- Java中的instanceof关键字