LeetCode 1793. Maximum Score of a Good Subarray Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
1793. Maximum Score of a Good Subarray

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

Solutions

Solution 1: Monotonic Stack

We can enumerate each element nums[i] in nums as the minimum value of the subarray, and use a monotonic stack to find the first position left[i] on the left that is less than nums[i] and the first position right[i] on the right that is less than or equal to nums[i]. Then, the score of the subarray with nums[i] as the minimum value is nums[i] × (right[i] - left[i] - 1).

It should be noted that the answer can only be updated when the left and right boundaries left[i] and right[i] satisfy left[i]+1 ≤ k ≤ right[i]-1.

The time complexity is O(n), and the space complexity is O(n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScript
class Solution: def maximumScore(self, nums: List[int], k: int) -> int: n = len(nums) left = [-1] * n right = [n] * n stk = [] for i, v in enumerate(nums): while stk and nums[stk[-1]] >= v: stk.pop() if stk: left[i] = stk[-1] stk.append(i) stk = [] for i in range(n - 1, -1, -1): v = nums[i] while stk and nums[stk[-1]] > v: stk.pop() if stk: right[i] = stk[-1] stk.append(i) ans = 0 for i, v in enumerate(nums): if left[i] + 1 <= k <= right[i] - 1: ans = max(ans, v * (right[i] - left[i] - 1)) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !