LeetCode Container With Most Water(011)解法總結

NewCoder1024發表於2020-03-17

描述

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.

LeetCode Container With Most Water(011)解法總結

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49複製程式碼

思路

生成一個二維陣列,計算每兩個擋板之間的存水量,時間複雜度均為O(n^2)。

class Solution {
    public int maxArea(int[] height) {
        int out = 0;
        for(int i = 0; i<height.length; i++){
            for(int j = 0; j<=i; j++){
                out = Math.max(out,((i - j) * Math.min(height[i], height[j])));
            }
        }
        return out;
    }
}複製程式碼
Runtime: 370 ms, faster than 9.73% of Java online submissions for Container With Most Water.
Memory Usage: 41.3 MB, less than 5.77% of Java online submissions for Container With Most Water.

可以看出時間消耗確實很高。

優化

評論區發現一種思路,當有一邊的擋板高度大於另一邊時,將高的那邊的index向中間收縮不會得到更大的值,因為寬和高一定都會縮短

The max area is calculated by the following formula:

S = (j - i) * min(ai, aj)

We should choose (i, j) so that S is max. Note that i, j go through the range (1, n) and j > i. That's it.

The simple way is to take all possibilities of (i, j) and compare all obtained S. The time complexity is n * (n-1) / 2

What we gonna do is to choose all possibilities of (i, j) in a wise way. I noticed that many submitted solutions here can't explain why when :

  • ai < aj we will check the next (i+1, j) (or move i to the right)
  • ai >= aj we will check the next (i, j-1) (or move j to the left)

Here is the explaination for that:

  • When ai < aj , we don't need to calculate all (i, j-1), (i, j-2), .... Why? because these max areas are smaller than our S at (i, j)

Proof: Assume at (i, j-1) we have S'= (j-1-i) * min(ai, aj-1)
S'< (j-1-i) * ai < (j-i) * ai = S, and when S'<S, we don't need to calculate
Similar at (i, j-2), (i, j-3), etc.

So, that's why when ai < aj, we should check the next at (i+1, j) (or move i to the right)

  • When ai >= aj, the same thing, all (i+1, j), (i+2, j), .... are not needed to calculate.

We should check the next at (i, j-1) (or move j to the left)

下面是實現:

class Solution {
    public int maxArea(int[] height) {
        int i = 0, j = height.length -1, s = 0;
        while(i != j){
            s = Math.max(s, ((j - i) * Math.min(height[i], height[j])));
            if(height[i] < height[j]){
                i++;
            }else{
                j--;
            }
        }
        return s;
    }
}複製程式碼
Runtime: 2 ms, faster than 95.46% of Java online submissions for Container With Most Water.
Memory Usage: 41.4 MB, less than 5.77% of Java online submissions for Container With Most Water.

避免迴圈還是能夠節約很多時間的。

相關文章