1793. Maximum Score of a Good Subarray
On This Page
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)
res = 0
for i, v in enumerate(nums):
if left[i] + 1 <= k <= right[i] - 1:
res = max(res, v * (right[i] - left[i] - 1))
return res