We need to find the subarray that includes the index k (from i to j where i <= k <= j) and maximizes the score defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1).
Here is the approach:
Start with the subarray that only contains the element at index k.
Expand the subarray to the left and right, one element at a time, while keeping track of the minimum element in the subarray.
For each expansion, calculate the score and keep track of the maximum score encountered.
classSolution:
defmaximumScore(self, nums, k):
n = len(nums)
left = right = k
minElement = nums[k]
maxScore = minElement
while left >0or right < n -1:
if left ==0:
right +=1elif right == n -1:
left -=1elif nums[left -1] < nums[right +1]:
right +=1else:
left -=1 minElement = min(minElement, min(nums[left], nums[right]))
currentScore = minElement * (right - left +1)
maxScore = max(maxScore, currentScore)
return maxScore