LeetCode solutions

3176. Minimum Sum of Mountain Triplets I

My accepted Python solution to LeetCode problem 3176, Minimum Sum of Mountain Triplets I, running in 40ms.

  • Difficulty: Easy
  • Python
  • Runtime 40ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 40ms, memory 17.3MB, accepted 2026-01-02.

python
class Solution:
    def minimumSum(self, nums: List[int]) -> int:
        n = len(nums)
        if n < 3:
            return -1
        left_min = [float('inf')] * n
        left_min[0] = nums[0]
        for i in range(1, n):
            left_min[i] = min(left_min[i-1], nums[i])
        right_min = [float('inf')] * n
        right_min[n-1] = nums[n-1]
        for i in range(n-2, -1, -1):
            right_min[i] = min(right_min[i+1], nums[i])
        result = float('inf')
        for j in range(1, n-1):
            if left_min[j-1] < nums[j] and right_min[j+1] < nums[j]:
                result = min(result, left_min[j-1] + nums[j] + right_min[j+1])
        return result if result != float('inf') else -1

Source