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
Minimum Sum of Mountain Triplets I INPUT nums array (0-indexed) 8 i=0 6 i=1 1 i=2 5 i=3 3 i=4 Mountain Triplet Pattern: nums[i] nums[j] nums[k] Conditions: i < j < k nums[i] < nums[j] > nums[k] Find: min(nums[i]+nums[j]+nums[k]) nums = [8,6,1,5,3] ALGORITHM STEPS 1 Left Min Array leftMin[j] = min value left of j [INF, 8, 6, 1, 1] 2 Right Min Array rightMin[j] = min value right of j [1, 1, 3, 3, INF] 3 Check Each Peak j For valid peak: leftMin[j] < nums[j] AND rightMin[j] < nums[j] 4 Calculate Min Sum sum = leftMin[j] + nums[j] + rightMin[j] j=1: 8+6+1 = 15 (valid) j=2: 6+1+3 = X (1 not peak) j=3: 1+5+3 = 9 (valid, MIN!) j=4: no right element Minimum = 9 FINAL RESULT Optimal Mountain Triplet Found: 1 i=2 5 j=3 (peak) 3 k=4 1 + 5 + 3 = 9 nums[2]+nums[3]+nums[4] OUTPUT 9 OK - Valid mountain found! Key Insight: Two-Pass Optimization: Pre-compute leftMin[] and rightMin[] arrays in O(n) time each. For each potential peak j, we instantly know the minimum values on both sides, reducing brute force O(n^3) to efficient O(n) time complexity with O(n) space. TutorialsPoint - Minimum Sum of Mountain Triplets I | Two-Pass Optimization Approach
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