Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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] and nums[k] < nums[j]Return the minimum possible sum of a mountain triplet of nums. If no such triplet exists, return -1.
Example 1:
Input: nums = [8,6,1,5,3] Output: 9 Explanation: Triplet (2, 3, 4) is a mountain triplet of sum 9 since: - 2 < 3 < 4 - nums[2] < nums[3] and nums[4] < nums[3] And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
Example 2:
Input: nums = [5,4,8,7,10,2] Output: 13 Explanation: Triplet (1, 3, 5) is a mountain triplet of sum 13 since: - 1 < 3 < 5 - nums[1] < nums[3] and nums[5] < nums[3] And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
Example 3:
Input: nums = [6,5,4,3,4,5] Output: -1 Explanation: It can be shown that there are no mountain triplets in nums.
Constraints:
3 <= nums.length <= 1051 <= nums[i] <= 108Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[8,6,1,5,3]
[5,4,8,7,10,2]
[6,5,4,3,4,5]
3sum)maximum-value-of-an-ordered-triplet-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2909: Minimum Sum of Mountain Triplets II
class Solution {
public int minimumSum(int[] nums) {
int n = nums.length;
int[] right = new int[n + 1];
final int inf = 1 << 30;
right[n] = inf;
for (int i = n - 1; i >= 0; --i) {
right[i] = Math.min(right[i + 1], nums[i]);
}
int ans = inf, left = inf;
for (int i = 0; i < n; ++i) {
if (left < nums[i] && right[i + 1] < nums[i]) {
ans = Math.min(ans, left + nums[i] + right[i + 1]);
}
left = Math.min(left, nums[i]);
}
return ans == inf ? -1 : ans;
}
}
// Accepted solution for LeetCode #2909: Minimum Sum of Mountain Triplets II
func minimumSum(nums []int) int {
n := len(nums)
const inf = 1 << 30
right := make([]int, n+1)
right[n] = inf
for i := n - 1; i >= 0; i-- {
right[i] = min(right[i+1], nums[i])
}
ans, left := inf, inf
for i, x := range nums {
if left < x && right[i+1] < x {
ans = min(ans, left+x+right[i+1])
}
left = min(left, x)
}
if ans == inf {
return -1
}
return ans
}
# Accepted solution for LeetCode #2909: Minimum Sum of Mountain Triplets II
class Solution:
def minimumSum(self, nums: List[int]) -> int:
n = len(nums)
right = [inf] * (n + 1)
for i in range(n - 1, -1, -1):
right[i] = min(right[i + 1], nums[i])
ans = left = inf
for i, x in enumerate(nums):
if left < x and right[i + 1] < x:
ans = min(ans, left + x + right[i + 1])
left = min(left, x)
return -1 if ans == inf else ans
// Accepted solution for LeetCode #2909: Minimum Sum of Mountain Triplets II
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2909: Minimum Sum of Mountain Triplets II
// class Solution {
// public int minimumSum(int[] nums) {
// int n = nums.length;
// int[] right = new int[n + 1];
// final int inf = 1 << 30;
// right[n] = inf;
// for (int i = n - 1; i >= 0; --i) {
// right[i] = Math.min(right[i + 1], nums[i]);
// }
// int ans = inf, left = inf;
// for (int i = 0; i < n; ++i) {
// if (left < nums[i] && right[i + 1] < nums[i]) {
// ans = Math.min(ans, left + nums[i] + right[i + 1]);
// }
// left = Math.min(left, nums[i]);
// }
// return ans == inf ? -1 : ans;
// }
// }
// Accepted solution for LeetCode #2909: Minimum Sum of Mountain Triplets II
function minimumSum(nums: number[]): number {
const n = nums.length;
const right: number[] = Array(n + 1).fill(Infinity);
for (let i = n - 1; ~i; --i) {
right[i] = Math.min(right[i + 1], nums[i]);
}
let [ans, left] = [Infinity, Infinity];
for (let i = 0; i < n; ++i) {
if (left < nums[i] && right[i + 1] < nums[i]) {
ans = Math.min(ans, left + nums[i] + right[i + 1]);
}
left = Math.min(left, nums[i]);
}
return ans === Infinity ? -1 : ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.