Description
You are given an array of integers nums
(0-indexed) and an integer k
.
The score of a subarray (i, j)
is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1)
. A good subarray is a subarray where i <= k <= j
.
Return the maximum possible score of a good subarray.
Example 1:
Input: nums = [1,4,3,7,4,5], k = 3 Output: 15 Explanation: The optimal subarray is (1, 5) with a score of min(4,3,7,4,5) * (5-1+1) = 3 * 5 = 15.
Example 2:
Input: nums = [5,5,4,5,4,1,1,1], k = 0 Output: 20 Explanation: The optimal subarray is (0, 4) with a score of min(5,5,4,5,4) * (4-0+1) = 4 * 5 = 20.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 2 * 104
0 <= k < nums.length
Code
Two Pointer
Time Complexity: , Space Complexity:
Intuition:我們想要 mini 盡可能的大,且 j - i + 1
盡可能的長。因此由 index k 開始,往左右兩側延伸,取大者(因為要 mini 盡可能的大)。
這個 intuition (i, j 向左向右移動的優先順序)類似 Container With Most Water,只是在這題是由內往兩側延伸,而在 Container With Most Water 中是由兩側往中心點去找。
We start from k
and track the current maximum score. We have two choices - expand left or right. To maximize the score, we expand towards the higher number.
Monotonic Stack
Time Complexity: , Space Complexity:
其實就是在找 Largest Rectangle in Histogram。只是多了一個 constraint: if(idx < k && i > k)
。