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 and n is at least 2.
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
int my_max(int a,int b)
{
return a>b?a:b;
}
class Solution {
public:
int maxArea(vector<int>& height) {
int head = 0;
int tail = height.size()-1;
int max_s = (height[head]<height[tail]?height[head]:height[tail])*(tail-head);
while(head<tail)
{
max_s = my_max(max_s, (height[head]<height[tail]?height[head]:height[tail])*(tail-head));
if(height[head]>height[tail])
tail--;
else
head++;
}
return max_s;
}
};
附证明:
参考https://segmentfault.com/a/1190000008824222
以下补充一个简单的反证法证明算法的合理性
当前的算法为:使用两个指针分别指向数组的头和尾。指向的值较小的那个指针移动,即左指针右移,右指针左移。当左右指针相遇时,指针
假设:该算法并没有遍历到容量最大的情况
我们令容量最大时的指针为p_left和p_right。根据题设,我们可以假设遍历时左指针先到达p_left,但是当左指针为p_left时,右指针还没有经过p_right左指针就移动了
已知当左指针停留在p_left时,它只有在两种场景下会发生改变
-
左指针和右指针在p_left相遇,则右指针一定在前往p_left的途中经过p_right,与题设矛盾
-
右指针位于p_right右侧且当前的值大于左指针。则在这种情况下,此时容器的盛水量比题设中最大的盛水量还要大,与题设矛盾
因此该算法的遍历一定经过了最大的盛水量的情况 -
对右指针先到达p_right的情况证明同理。