2908. Minimum Sum of Mountain Triplets I

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2908

class Solution:
    def minimumSum(self, nums: List[int]) -> int:
        n = len(nums)
        right = [inf] * (n + 1)
        for i in range(n - 1, -1, -1):
            right[i] = min(right[i + 1], nums[i])
        res = left = inf
        for i, x in enumerate(nums):
            if left < x and right[i + 1] < x:
                res = min(res, left + x + right[i + 1])
            left = min(left, x)
        return -1 if res == inf else res