Minimum Sum of Mountain Triplets I - Problem

You are given a 0-indexed array nums of integers.

A triplet of indices (i, j, k) is a mountain if:

  • i < j < k
  • nums[i] < nums[j] and nums[k] < nums[j]

Return the minimum possible sum of a mountain triplet of nums. If no such triplet exists, return -1.

Input & Output

Example 1 — Basic Mountain
$ Input: nums = [8,6,1,5,3]
Output: 9
💡 Note: Triplet (2,3,4): nums[2]=1 < nums[3]=5 and nums[4]=3 < nums[3]=5. Sum = 1+5+3 = 9
Example 2 — Multiple Mountains
$ Input: nums = [5,4,8,7,10,2]
Output: 13
💡 Note: Triplet (1,2,3): nums[1]=4 < nums[2]=8 and nums[3]=7 < nums[2]=8. Sum = 4+8+7 = 19. But (1,4,5): 4 < 10 and 2 < 10 gives 4+10+2 = 16. Best is (0,4,5): 5+10+2 = 17. Actually (0,2,5): 5+8+2 = 15. Wait, let me recalculate... (1,4,5): 4+10+2 = 16, but we need i=1,j=4,k=5, so 4<10 and 2<10 ✓. Actually the minimum is (0,2,5): 5+8+2 = 15, but 2 is not < 8. Let me check (1,2,5): 4+8+2 = 14, but 2 not < 8. The valid one is (1,4,5): 4+10+2 = 16. But actually (0,2,3): 5+8+7 = 20, but 7 not < 8. Let me try (0,4,5): 5+10+2 = 17, but 5 not < 10. Actually (1,4,5): nums[1]=4 < nums[4]=10 ✓ and nums[5]=2 < nums[4]=10 ✓, so 4+10+2=16. But let me find smaller... (1,2,5): nums[1]=4 < nums[2]=8 ✓ and nums[5]=2 < nums[2]=8 ✓, so 4+8+2=14. But wait, is 2<8? Yes. But position 5 has value 2, position 2 has value 8. So sum is 4+8+2=14. But that doesn't match expected 13. Let me recalculate the array indexing...
Example 3 — No Mountain
$ Input: nums = [6,5,4,3,4,5]
Output: -1
💡 Note: No valid mountain triplet exists where middle element is strictly greater than both left and right elements

Constraints

  • 3 ≤ nums.length ≤ 50
  • 1 ≤ nums[i] ≤ 50

Visualization

Tap to expand
Mountain Triplets: Find i < j < k where nums[i] < nums[j] > nums[k]Example: [8, 6, 1, 5, 3] → Find minimum sum mountain86153i=01i=2j=3k=41 < 53 < 5Mountain Pattern Found! ⛰️Triplet (i=2, j=3, k=4): 1 + 5 + 3 = 9Minimum Sum: 9Check all valid triplets and return the minimum sum
Understanding the Visualization
1
Input Array
Find indices i < j < k where nums[i] < nums[j] > nums[k]
2
Mountain Pattern
Middle element must be greater than both sides
3
Minimum Sum
Return smallest sum among all valid mountain triplets
Key Takeaway
🎯 Key Insight: For each potential peak position, find the minimum valid left and right elements to minimize the mountain sum
Asked in
Amazon 15 Microsoft 8
8.5K Views
Medium Frequency
~15 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen