Medium
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 the line i
is at (i, ai)
and (i, 0)
. Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
Notice that you may not slant the container.
Example 1:
Example 2:
Example 3:
Example 4:
Constraints:
n = height.length
2 <= n <= 3 * 104
0 <= height[i] <= 3 * 104
暴力解法,遍歷所有可能的長度組合算出可得到水量最大的長度組合。
我們先從最兩側的直線開始計算水量並記錄目前最大水量變數 maxWater
中,接著我們可以這樣想,左右兩側接下來移動哪邊才可能有更大的水量?
因為水量的多寡取決於兩側直線間的距離和兩側直線中較短的那側的長度,所以如果我們往中間移動較長的那側有兩種可能,一是下個直線較長,但因為這時兩側中較短直線的長度不變且直線間的距離還縮短所以不可能得到更大的水量。
反過來如果我們往中間移動較短那側的直線一樣有兩種可能,一是下個直線更短,但另一個可能是下個直線更長,如果下個直線增加的長度大於減少的距離時我們就可以得到更大的水量。
因此我們一律從較短的長度那側往中間移動到下個直線並計算當下的水量,如果大於 maxWater
則更新它,反覆進行直到兩邊指向的直線重疊為止。
時間複雜度:用 Two Pointers 邊計算邊往中間移動,只需檢查過所有直線 1 次,所以如果有 條直線,時間複雜度為
空間複雜度:只用到 maxWater
、left
、right
3 個變數,所以空間複雜度為