每日leetcode--(11) Container With Most Water

Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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时,它只有在两种场景下会发生改变

  1. 左指针和右指针在p_left相遇,则右指针一定在前往p_left的途中经过p_right,与题设矛盾

  2. 右指针位于p_right右侧且当前的值大于左指针。则在这种情况下,此时容器的盛水量比题设中最大的盛水量还要大,与题设矛盾
    因此该算法的遍历一定经过了最大的盛水量的情况

  3. 对右指针先到达p_right的情况证明同理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值