Skip to main content

2909. Minimum Sum of Mountain Triplets II

Link

class Solution:
def minimumSum(self, nums: List[int]) -> int:

N = len(nums)
INF = 10 ** 20

def gen(lst):

res = [INF]

for i in range(len(lst)):
res.append(min(lst[i], res[-1]))

return res

prefix = gen(nums)
suffix = gen(nums[::-1])[::-1]

res = INF

for i in range(1, N-1):
if prefix[i] < nums[i] and nums[i] > suffix[i+1]:
res = min(res, prefix[i] + nums[i] + suffix[i+1])

return res if res != INF else -1