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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer array nums of length n.
A trionic subarray is a contiguous subarray nums[l...r] (with 0 <= l < r < n) for which there exist indices l < p < q < r such that:
nums[l...p] is strictly increasing,nums[p...q] is strictly decreasing,nums[q...r] is strictly increasing.Return the maximum sum of any trionic subarray in nums.
Example 1:
Input: nums = [0,-2,-1,-3,0,2,-1]
Output: -4
Explanation:
Pick l = 1, p = 2, q = 3, r = 5:
nums[l...p] = nums[1...2] = [-2, -1] is strictly increasing (-2 < -1).nums[p...q] = nums[2...3] = [-1, -3] is strictly decreasing (-1 > -3)nums[q...r] = nums[3...5] = [-3, 0, 2] is strictly increasing (-3 < 0 < 2).(-2) + (-1) + (-3) + 0 + 2 = -4.Example 2:
Input: nums = [1,4,2,7]
Output: 14
Explanation:
Pick l = 0, p = 1, q = 2, r = 3:
nums[l...p] = nums[0...1] = [1, 4] is strictly increasing (1 < 4).nums[p...q] = nums[1...2] = [4, 2] is strictly decreasing (4 > 2).nums[q...r] = nums[2...3] = [2, 7] is strictly increasing (2 < 7).1 + 4 + 2 + 7 = 14.Constraints:
4 <= n = nums.length <= 105-109 <= nums[i] <= 109Problem summary: You are given an integer array nums of length n. A trionic subarray is a contiguous subarray nums[l...r] (with 0 <= l < r < n) for which there exist indices l < p < q < r such that: nums[l...p] is strictly increasing, nums[p...q] is strictly decreasing, nums[q...r] is strictly increasing. Return the maximum sum of any trionic subarray in nums.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[0,-2,-1,-3,0,2,-1]
[1,4,2,7]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3640: Trionic Array II
class Solution {
public long maxSumTrionic(int[] nums) {
int n = nums.length;
int i = 0;
long ans = Long.MIN_VALUE;
while (i < n) {
int l = i;
i += 1;
while (i < n && nums[i - 1] < nums[i]) {
i += 1;
}
if (i == l + 1) {
continue;
}
int p = i - 1;
long s = nums[p - 1] + nums[p];
while (i < n && nums[i - 1] > nums[i]) {
s += nums[i];
i += 1;
}
if (i == p + 1 || i == n || nums[i - 1] == nums[i]) {
continue;
}
int q = i - 1;
s += nums[i];
i += 1;
long mx = 0, t = 0;
while (i < n && nums[i - 1] < nums[i]) {
t += nums[i];
i += 1;
mx = Math.max(mx, t);
}
s += mx;
mx = 0;
t = 0;
for (int j = p - 2; j >= l; j--) {
t += nums[j];
mx = Math.max(mx, t);
}
s += mx;
ans = Math.max(ans, s);
i = q;
}
return ans;
}
}
// Accepted solution for LeetCode #3640: Trionic Array II
func maxSumTrionic(nums []int) int64 {
n := len(nums)
i := 0
ans := int64(math.MinInt64)
for i < n {
l := i
for i++; i < n && nums[i-1] < nums[i]; {
i++
}
if i == l+1 {
continue
}
p := i - 1
s := int64(nums[p-1]) + int64(nums[p])
for i < n && nums[i-1] > nums[i] {
s += int64(nums[i])
i++
}
if i == p+1 || i == n || nums[i-1] == nums[i] {
continue
}
q := i - 1
s += int64(nums[i])
i++
var mx, t int64
for i < n && nums[i-1] < nums[i] {
t += int64(nums[i])
i++
mx = max(mx, t)
}
s += mx
mx, t = 0, 0
for j := p - 2; j >= l; j-- {
t += int64(nums[j])
mx = max(mx, t)
}
s += mx
ans = max(ans, s)
i = q
}
return ans
}
# Accepted solution for LeetCode #3640: Trionic Array II
class Solution:
def maxSumTrionic(self, nums: List[int]) -> int:
n = len(nums)
i = 0
ans = -inf
while i < n:
l = i
i += 1
while i < n and nums[i - 1] < nums[i]:
i += 1
if i == l + 1:
continue
p = i - 1
s = nums[p - 1] + nums[p]
while i < n and nums[i - 1] > nums[i]:
s += nums[i]
i += 1
if i == p + 1 or i == n or nums[i - 1] == nums[i]:
continue
q = i - 1
s += nums[i]
i += 1
mx = t = 0
while i < n and nums[i - 1] < nums[i]:
t += nums[i]
i += 1
mx = max(mx, t)
s += mx
mx = t = 0
for j in range(p - 2, l - 1, -1):
t += nums[j]
mx = max(mx, t)
s += mx
ans = max(ans, s)
i = q
return ans
// Accepted solution for LeetCode #3640: Trionic Array II
impl Solution {
pub fn max_sum_trionic(nums: Vec<i32>) -> i64 {
let n = nums.len();
let mut i = 0;
let mut ans = i64::MIN;
while i < n {
let l = i;
i += 1;
while i < n && nums[i - 1] < nums[i] {
i += 1;
}
if i == l + 1 {
continue;
}
let p = i - 1;
let mut s = nums[p - 1] as i64 + nums[p] as i64;
while i < n && nums[i - 1] > nums[i] {
s += nums[i] as i64;
i += 1;
}
if i == p + 1 || i == n || nums[i - 1] == nums[i] {
continue;
}
let q = i - 1;
s += nums[i] as i64;
i += 1;
let mut mx = 0i64;
let mut t = 0i64;
while i < n && nums[i - 1] < nums[i] {
t += nums[i] as i64;
i += 1;
mx = mx.max(t);
}
s += mx;
mx = 0;
t = 0;
let mut j = p as isize - 2;
while j >= l as isize {
t += nums[j as usize] as i64;
mx = mx.max(t);
j -= 1;
}
s += mx;
ans = ans.max(s);
i = q;
}
ans
}
}
// Accepted solution for LeetCode #3640: Trionic Array II
function maxSumTrionic(nums: number[]): number {
const n = nums.length;
let i = 0;
let ans = -Infinity;
while (i < n) {
const l = i;
i += 1;
while (i < n && nums[i - 1] < nums[i]) i += 1;
if (i === l + 1) continue;
const p = i - 1;
let s = nums[p - 1] + nums[p];
while (i < n && nums[i - 1] > nums[i]) {
s += nums[i];
i += 1;
}
if (i === p + 1 || i === n || nums[i - 1] === nums[i]) continue;
const q = i - 1;
s += nums[i];
i += 1;
let mx = 0,
t = 0;
while (i < n && nums[i - 1] < nums[i]) {
t += nums[i];
i += 1;
mx = Math.max(mx, t);
}
s += mx;
mx = 0;
t = 0;
for (let j = p - 2; j >= l; j--) {
t += nums[j];
mx = Math.max(mx, t);
}
s += mx;
ans = Math.max(ans, s);
i = q;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
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.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.