leetcode 11 Container With Most Water
来源:互联网 发布:一号店软件吧 编辑:程序博客网 时间:2024/05/01 22:05
Container With Most Water
Total Accepted: 48739 Total Submissions: 153072Given 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.
这道题意思很明确,就是给出一些点,然后将这些点与x轴做垂线,这样就形成许多条垂线段,把这些垂线段想象成木桶的桶壁,x轴想象成木桶的桶底,题目要求在其中选出两个桶壁,使得形成的木桶装水的体积最大。
这道题最直观的想法就是搜索所有可能的桶壁的组合,找出使得装水体积最大的组合。但这样一来,就是
该方法的主要过程是这样的,一头一尾用两个指针,计算两者形成的木桶的装水体积。然后选两者较小的一个,把它往靠近中间位置移一格,如果,新的桶壁高度不如之前移动的桶壁,那么就再往中间位置移一格,重复这个过程,直到新的桶壁高度高于移动的桶壁。然后再计算新形成的木桶的装水的体积。然后再选择两者较小的一个,把它往靠近中间位置移一格,重复上面的过程,直到两个指针重合。此时的最大体积,就是我们要求的所有可能组合中的最大体积。
我们用一个例子来说明这个过程:
总共6块桶壁,那么我们首先计算首尾两个桶壁形成的木桶的装水体积
然后我们比较两个桶壁的大小,找出小的一个,然后向中间移动一格。此时我们要移动的就是指向6号桶壁的指针,让它指向5号桶壁,这样一次移动其实就排除了2-6,3-6, 4-6,5-6四种组合,因为这四种组合不可能体积大于1-6. 原因很简单,首先底边长度小于1-6组合,其次由于都和6号桶壁组合所以高度最多为4(即6号桶壁的高度)而1-6组合的高度就是4,所以乘起来的结果必然小于1-6组合,所以直接淘汰掉。下面比较新指向的5号桶壁和6号桶壁的高度,如果5号桶壁的高度低于等于6号桶壁的高度那么5号桶壁淘汰,因为所有与5号桶壁的组合都有一个对应的与6号桶壁的组合,并且底边比其长1。例如:1-5对应1-6, 2-5对应2-6, 3-5对应3-6… …但在高度上1-5组合为min(h1,h5),1-6组合为min(h1,h6),而h5
接下来比较3号桶壁和5号桶壁的高度,发现5号桶壁较矮,所以指向5号桶壁的指针改指向4号桶壁,5号桶壁被淘汰,对应的4-5组合被排除,比较4号桶壁和之前的桶壁(即5号桶壁),4号桶壁比较高,所以重新计算体积,体积为7. 因此最大体积仍为20,然后比较3号桶壁和4号桶壁,发现3号桶壁较矮,因此移动指向3号桶壁的指针指向4号桶壁,3号桶壁被淘汰(注意:我所说的淘汰是指它没有机会产生比当前最大值更大的体积)。此时首尾两个指针都指向4号桶壁,指针重合,因此退出循环。当前的最大体积就是所有可能组合的最大体积。
下面是程序的完整代码:
#include <iostream>#include <stdlib.h>#include <stdio.h>using namespace std;int maxArea(int* height, int heightSize) { if(heightSize < 2) return 0; int indexl = 0, indexr = heightSize - 1; int max = 0, tmpArea = 0; int currentHeight = 0; while(indexl < indexr) { if(height[indexl] < height[indexr]) { tmpArea = height[indexl] * (indexr - indexl); max = max < tmpArea ? tmpArea : max; currentHeight = height[indexl]; indexl++; while(indexl < indexr && height[indexl] <= currentHeight) { indexl++; } } else { tmpArea = height[indexr] * (indexr - indexl); max = max < tmpArea ? tmpArea : max; currentHeight = height[indexr]; indexr--; while(indexl < indexr && height[indexr] <= currentHeight) { indexr--; } } } return max;}int main(){ int heightSize; cout<<"Input the heightSize:"<<endl; cin>>heightSize; int *height = (int*)malloc(sizeof(int) * heightSize); for(int i = 0; i < heightSize; i++) scanf("%d,", &height[i]); cout<<maxArea(height, heightSize)<<endl; return 0;}
- LeetCode 11: Container With Most Water
- leetcode 11 Container With Most Water
- Leetcode【11】:Container With Most Water
- [leetcode 11] Container With Most Water
- [leetcode 11] Container With Most Water
- LeetCode | #11 Container With Most Water
- leetcode.11---------Container With Most Water
- [LeetCode 11]Container With Most Water
- leetcode 11 Container With Most Water
- leetcode-11 Container With Most Water
- Container With Most Water - LeetCode 11
- [Leetcode 11, Medium] Container With Most Water
- LeetCode 11 - Container With Most Water
- leetcode 11 -- Container With Most Water
- #LeetCode OJ #11 Container With Most Water
- Leetcode #11 Container With Most Water
- leetcode-11Container With Most Water
- leetcode 11 -- Container With Most Water
- AngularJS入门教程05:XHR和依赖注入
- CF#317Div.2,ProblemB,水题
- 阿里巴巴2016校招在线笔试附加题(C++研发)
- AngularJS入门教程06:链接与图片模板
- 8-21
- leetcode 11 Container With Most Water
- hdu5391 Zball in Tina Town(打表找规律)
- Burp Suite抓HTTPS数据包(通用)
- 软件测试中条件覆盖,路径覆盖,语句覆盖,分支覆盖的区别
- AngularJS入门教程07:路由与多视图
- 访问控制列表
- AngularJS入门教程08:更多模板
- popen() 函数用法
- PIC配置位之STVREN