LeetCode 11. Container With Most Water

来源:互联网 发布:jieba 词性标注 java 编辑:程序博客网 时间:2024/06/18 18:15

  • 题目
  • 题意
  • 分析
    • round 1
    • round 2
    • round 3
    • round 4 5
  • 代码

题目

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
原创粉丝点击