Minimum Sum of Mountain Triplets II - Problem
You are given a 0-indexed array nums of integers.
A triplet of indices (i, j, k) is a mountain if:
i < j < knums[i] < nums[j]andnums[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) forms a mountain: nums[2]=1 < nums[3]=5 > nums[4]=3. Sum = 1+5+3 = 9.
Example 2 — Multiple Mountains
$
Input:
nums = [5,4,8,7,10,2]
›
Output:
13
💡 Note:
Multiple valid mountains exist. Optimal is (1,3,5): nums[1]=4 < nums[3]=7 > nums[5]=2. Sum = 4+7+2 = 13.
Example 3 — No Mountain
$
Input:
nums = [6,5,4,3,4,5]
›
Output:
-1
💡 Note:
No valid mountain triplet exists. Elements either increase or decrease monotonically in segments, preventing mountain formation.
Constraints
- 3 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 108
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code