Skip to main content

209. Minimum Size Subarray Sum

Medium
Description

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are 1(i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

Example 1:​

Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.

Example 2:​

Input: target = 4, nums = [1,4,4]
Output: 1

Example 3:​

Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0

Constraints:​

1 <= target <= 109
1 <= nums.length <= 105
1 <= nums[i] <= 104

Follow up: If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)).

解題思路​

練習 Sliding Window
首先由 right 開始慢慢往右移動,並加總 left 和 right 範圍的這個 window 中的總和,直到大於 target。
當大於 target 時候 left 就往右移動來減少 minLength 的可能值,重複移動 left 和 right 直到找出正確答案。

心得​

也是看了 Wilson Ren 的 資料結構與演算法 (JavaScript) 介紹這題經典題,要把這些題型弄熟打好演算法基礎!