leetcode Container With Most Water
2014-08-01 17:37
302 查看
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.
两边设一个指针,然后计算area,如果height[i] <= height[j],那么i++,因为在这里height[i]是瓶颈,j往里移只会减少面积,不会再增加area。
这是一个贪心的策略,每次取两边围栏最矮的一个推进,希望获取更多的水。
可以做简单证明。
如图,L1<R1<R2<L2,那么在L1移动到L2和R1移动到R2的过程中,存在的Lx和Rx都满足Lx < L1, Rx < R1。假设Lx < Rx,那么Lx* d(Rx - Lx) < L1*d(Rx-Lx) < L1 * ( R1 - L1)。若Lx > Rx,则Rx * d(Rx - Lx) < Rx * d(R1 - L1) < L1 * d(R1 - L1) (因为Rx,Lx都小于L1<R1)。直观的想就是,如果宽度减少,同时左右两侧的板都比原来的还要矮,怎么可能容纳更多的水呢?
![](http://images.cnitblog.com/blog/545749/201307/27140406-419b7981438f4738b3f8d629678d0a45.png)
class Solution {
public:
int maxArea(vector<int> &height) {
int i = 0;
int j = height.size()-1;
int max = 0;
int area = 0;
while(i<j) {
area = (j-i) * (height[j]>=height[i]? height[i] : height[j]);
if(area>max)
max = area;
if(height[j]>=height[i])
i++;
else
j--;
}
return max;
}
};
..., 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.
两边设一个指针,然后计算area,如果height[i] <= height[j],那么i++,因为在这里height[i]是瓶颈,j往里移只会减少面积,不会再增加area。
这是一个贪心的策略,每次取两边围栏最矮的一个推进,希望获取更多的水。
可以做简单证明。
如图,L1<R1<R2<L2,那么在L1移动到L2和R1移动到R2的过程中,存在的Lx和Rx都满足Lx < L1, Rx < R1。假设Lx < Rx,那么Lx* d(Rx - Lx) < L1*d(Rx-Lx) < L1 * ( R1 - L1)。若Lx > Rx,则Rx * d(Rx - Lx) < Rx * d(R1 - L1) < L1 * d(R1 - L1) (因为Rx,Lx都小于L1<R1)。直观的想就是,如果宽度减少,同时左右两侧的板都比原来的还要矮,怎么可能容纳更多的水呢?
![](http://images.cnitblog.com/blog/545749/201307/27140406-419b7981438f4738b3f8d629678d0a45.png)
class Solution {
public:
int maxArea(vector<int> &height) {
int i = 0;
int j = height.size()-1;
int max = 0;
int area = 0;
while(i<j) {
area = (j-i) * (height[j]>=height[i]? height[i] : height[j]);
if(area>max)
max = area;
if(height[j]>=height[i])
i++;
else
j--;
}
return max;
}
};
相关文章推荐
- LeetCode Container With Most Water
- 【Leetcode】Container With Most Water
- leetcode Container With Most Water
- [LeetCode]Container With Most Water, 解题报告
- [leetcode] Container With Most Water
- LeetCode题解——Container With Most Water
- [LeetCode] Container With Most Water
- [LeetCode]Container With Most Water
- LeetCode 之 Container With Most Water — C 实现
- Leetcode -- Python --Container With Most Water
- leetcode 11 Container with Most Water
- LeetCode-Container With Most Water
- LeetCode——Container With Most Water
- 最大容积--Container With Most Water of leetcode
- LeetCode Solutions : Container With Most Water
- LeetCode 11 Container With Most Water
- leetcode:11. Container With Most Water
- LeetCode 之 Container With Most Water
- LeetCode算法问题6 —— Container With Most Water
- 【LeetCode】Container With Most Water