Minimum Sum of Mountain Triplets I - Problem

Imagine you're a mountain climber looking for the perfect mountain triplet - three peaks where the middle one towers above the others! ๐Ÿ”๏ธ

Given a 0-indexed array nums of integers, you need to find a triplet of indices (i, j, k) that forms a "mountain" pattern where:

  • i < j < k (indices are in order)
  • nums[i] < nums[j] (left peak is shorter than middle)
  • nums[k] < nums[j] (right peak is shorter than middle)

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

Example: In array [8,6,1,5,3], the triplet (2,3,4) gives us values (1,5,3) - a perfect mountain with sum 9!

Input & Output

example_1.py โ€” Basic Mountain
$ Input: [8,6,1,5,3]
โ€บ Output: 9
๐Ÿ’ก Note: We can form a mountain with indices (2,3,4): values (1,5,3). Since 1 < 5 and 3 < 5, this is a valid mountain with sum 1+5+3 = 9. This is the minimum possible sum.
example_2.py โ€” Multiple Mountains
$ Input: [5,4,8,7,10,2]
โ€บ Output: 13
๐Ÿ’ก Note: Several mountain triplets exist: (1,2,3) gives (4,8,7) with sum 19, (0,4,5) gives (5,10,2) with sum 17, and (1,4,5) gives (4,10,2) with sum 16. But the minimum is (0,2,5) giving (5,8,2) with sum 15. Wait, let me recalculate: (0,2,5) gives 5+8+2=15, but we need to check if this forms a valid mountain: 5 < 8 โœ“ and 2 < 8 โœ“. Actually, the minimum valid mountain is (1,2,5) giving 4+8+2=14. Let me verify: 4 < 8 โœ“ and 2 < 8 โœ“. The answer should be 13 from triplet (0,2,3): 5+8+7=20? No, that's wrong. Let me recalculate properly: (1,4,5) = 4+10+2=16, (1,2,5) = 4+8+2=14, (0,4,5) = 5+10+2=17. The minimum valid mountain appears to be (1,2,5) = 4+8+2=14. The expected output suggests 13, so there must be another valid combination I missed.
example_3.py โ€” No Mountain
$ Input: [6,5,4,3,4,5]
โ€บ Output: -1
๐Ÿ’ก Note: Let's check all possible mountains: For any middle element to form a mountain, we need a smaller element on both sides. Looking at the array [6,5,4,3,4,5], we can find valid mountains like (2,4,5) giving (4,4,5) - but 4 is not less than 4, so invalid. Actually, (1,4,5) gives (5,4,5) which is invalid since 5 โ‰ฎ 4. Let me check (0,4,5): (6,4,5) - invalid since 6 โ‰ฎ 4. After checking systematically, there are valid mountains like (2,5,4) but that violates i < j < k. The correct triplet (1,5,4) is invalid due to index order. Actually, (3,4,5) gives indices in order with values (3,4,5): 3 < 4 โœ“ and 5 โ‰ฎ 4 โœ—. It appears no valid mountain exists, hence -1.

Visualization

Tap to expand
Mountain Peak Explorer8Cost: $86Cost: $61Cost: $1 โญ5Peak: $5๐Ÿš3Cost: $3 โญ๐ŸŽฏ Optimal Mountain Formation Discovered!Left Mountain: $1 + Peak: $5 + Right Mountain: $3 = $9 TotalPerfect mountain shape: 1 < 5 > 3 โœ…๐Ÿ”๏ธ Mission Complete: Minimum Cost = $9Algorithm: O(nยฒ) time | O(1) space
Understanding the Visualization
1
Survey the Terrain
Fly over the mountain range [8,6,1,5,3] and identify potential middle peaks
2
Check Peak Validity
For each potential middle peak, verify it's taller than at least one mountain on each side
3
Find Minimum Costs
For valid middle peaks, find the cheapest left and right mountains to minimize total expedition cost
4
Optimal Formation
The triplet (1,5,3) at indices (2,3,4) gives the minimum cost of 9 for a perfect mountain formation
Key Takeaway
๐ŸŽฏ Key Insight: For each potential middle peak, we only need to find the cheapest (minimum) valid mountains on both sides. This transforms a complex triplet search into an efficient two-sided minimum-finding problem!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each position we may scan up to n elements on the right, but with better practical performance

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using variables to track minimum values and current position

n
2n
โœ“ Linear Space

Constraints

  • 3 โ‰ค nums.length โ‰ค 50
  • 1 โ‰ค nums[i] โ‰ค 108
  • All elements in the array are positive integers
Asked in
Google 23 Amazon 18 Microsoft 15 Meta 12
78.2K Views
Medium Frequency
~15 min Avg. Time
1.8K 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