2
def nextSmallerElement(self, nums):3
nextSmaller = [None] * len(nums)4
stack = [[-sys.maxsize, -1]]5
for i in range(len(nums) - 1, -1, -1):6
while nums[i] <= stack[-1][0]:8
nextSmaller[i] = stack[-1][1]9
stack.append([nums[i], i])12
def previousSmallerElement(self, nums):13
previousSmaller = [None] * len(nums)14
stack = [[-sys.maxsize, -1]]15
for i in range(len(nums)):16
while nums[i] <= stack[-1][0]:18
previousSmaller[i] = stack[-1][1]19
stack.append([nums[i], i])20
return previousSmaller22
def maximumScore(self, nums: List[int], k: int) -> int:23
nextSmaller = self.nextSmallerElement(nums)24
previousSmaller = self.previousSmallerElement(nums)27
for idx, num in enumerate(nums):28
# previousSmaller[idx] (let's say i) and nextSmaller[idx] (let's say j) ensures that the element present at idx is the minimum in range (i -> j)29
i = previousSmaller[idx]36
score = max(score, num * (j - i + 1))