LeetCode 11. Container With Most Water

题目

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.


题意

给定n个非负整数a1,a2,…,an,其中每个表示坐标(i,ai)处的点。 绘制n条垂直线,使得线i的两个端点在(i,ai)和(i,0)处。 找到两条线,它们与x轴一起形成一个容器,使得容器含有最多的水。

注意:您不能倾斜容器,n至少为2。


分析

比如,给出数组[1,2,3,4,5,6,3],其坐标是:

                           |
                      |    |
                 |    |    |    
            |    |    |    |    |
       |    |    |    |    |    | 
__|____|____|____|____|____|____|____
  0    1    2    3    4    5    6

round 1

首先,我们选择最长的边,也就是最左和最右的边,即(0,1)和(6,3)组成的水槽,此时,水槽可以装的水最大为result = 1*6 = 6,此时,当前水槽高度currH = min(1,3) = 1。

                           |
                      |    |
                 |    |    |    
            |    |    |    |    |
       |    |    |    |    |    | 
__|||||||____
  0    1    2    3    4    5    6

round 2

然后,我们选择向中间靠拢,看能不能找到更高的currH,使得水槽能装更多的水。我们找到(1,2)(6,3),此时,currH = min(2,3) = 2,水槽面积为2*5 = 10,比之前的max大。所以,result = max(6,10) = 10。

                           |
                      |    |
                 |    |    |    
            |    |    |    |    |
       |||||| 
__|____||||||____
  0    1    2    3    4    5    6

round 3

我们继续往前找,我们找到(2,3)、(6,3),此时,currH = min(3,3) = 3,水槽面积为3*4 = 12,比之前的max大。所以,result = max(12,10) = 12。

                           |
                      |    |
                 |    |    |    
            |||||
       |    ||||| 
__|____|____|||||____
  0    1    2    3    4    5    6

round 4 & 5

继续往前找,(3,4)、(5,6),此时,currH = min(4,6) = 4,水槽面积为4*2 = 8,比之前的max小。

继续找,(4,5)、(5,6),此时,currH = min(5,6) = 5,水槽面积为5*1 = 5,比之前的max小。

这个时候,已经不能继续往里缩了,所以,最大的水槽面积是12

                           |                                               |                  
                      |    |                                          ||                  
                 |||                                     |    ||                  
            |    |||    |                           |    |    ||    |             
       |    |    |||    |                      |    |    |    ||    |             
__|____|____|____|||____|____           __|____|____|____|____||____|____         
  0    1    2    3    4    5    6                 0    1    2    3    4    5    6             

代码

int maxArea(vector<int>& height) {
    int i=0,j=height.size()-1,result = 0;
    while(i<j){
        int currH = min(height[i],height[j]);
        result = max(result,currH*(j-i));
        while(height[i]<=currH && i<j) i++;
        while(height[j]<=currH && i<j) j--;
    }
    return result;
}

49 / 49 test cases passed.
Runtime: 19 ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值