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 an array of integers nums.
Your task is to find the length of the longest subsequence seq of nums, such that the absolute differences between consecutive elements form a non-increasing sequence of integers. In other words, for a subsequence seq0, seq1, seq2, ..., seqm of nums, |seq1 - seq0| >= |seq2 - seq1| >= ... >= |seqm - seqm - 1|.
Return the length of such a subsequence.
Example 1:
Input: nums = [16,6,3]
Output: 3
Explanation:
The longest subsequence is [16, 6, 3] with the absolute adjacent differences [10, 3].
Example 2:
Input: nums = [6,5,3,4,2,1]
Output: 4
Explanation:
The longest subsequence is [6, 4, 2, 1] with the absolute adjacent differences [2, 2, 1].
Example 3:
Input: nums = [10,20,10,19,10,20]
Output: 5
Explanation:
The longest subsequence is [10, 20, 10, 19, 10] with the absolute adjacent differences [10, 10, 9, 9].
Constraints:
2 <= nums.length <= 1041 <= nums[i] <= 300Problem summary: You are given an array of integers nums. Your task is to find the length of the longest subsequence seq of nums, such that the absolute differences between consecutive elements form a non-increasing sequence of integers. In other words, for a subsequence seq0, seq1, seq2, ..., seqm of nums, |seq1 - seq0| >= |seq2 - seq1| >= ... >= |seqm - seqm - 1|. Return the length of such a subsequence.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[16,6,3]
[6,5,3,4,2,1]
[10,20,10,19,10,20]
longest-increasing-subsequence)longest-increasing-subsequence-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3409: Longest Subsequence With Decreasing Adjacent Difference
class Solution {
public int longestSubsequence(int[] nums) {
final int mx = Arrays.stream(nums).max().getAsInt();
// dp[num][diff] := the length of the longest subsequence ending in `num`
// s.t. the last absolute difference between consecutive elements is `diff`
int[][] dp = new int[mx + 1][mx + 1];
for (final int num : nums) {
for (int prev = 1; prev <= mx; ++prev) {
final int diff = Math.abs(num - prev);
dp[num][diff] = Math.max(dp[num][diff], dp[prev][diff] + 1);
}
// dp[num][diff] := max(dp[num][j]), where j >= diff
for (int j = mx - 1; j >= 0; --j)
dp[num][j] = Math.max(dp[num][j], dp[num][j + 1]);
}
return Arrays.stream(dp).mapToInt(row -> row[0]).max().getAsInt();
}
}
// Accepted solution for LeetCode #3409: Longest Subsequence With Decreasing Adjacent Difference
package main
import (
"math"
"slices"
)
// https://space.bilibili.com/206214
func longestSubsequence(nums []int) (ans int) {
mx := slices.Max(nums)
maxD := mx - slices.Min(nums)
f := make([][]int, mx+1)
for i := range f {
f[i] = make([]int, maxD+1)
for j := range f[i] {
f[i][j] = math.MinInt
}
}
for _, x := range nums {
fx := 1
for j := maxD; j >= 0; j-- {
if x-j >= 0 {
fx = max(fx, f[x-j][j]+1)
}
if x+j <= mx {
fx = max(fx, f[x+j][j]+1)
}
f[x][j] = fx
ans = max(ans, fx)
}
}
return
}
func longestSubsequence2(nums []int) (ans int) {
mx := slices.Max(nums)
maxD := mx - slices.Min(nums)
f := make([][]int, len(nums))
for i := range f {
f[i] = make([]int, maxD+2)
}
last := make([]int, mx+1)
for i := range last {
last[i] = -1
}
for i, x := range nums {
for j := maxD; j >= 0; j-- {
f[i][j] = max(f[i][j+1], 1)
if x-j >= 0 && last[x-j] >= 0 {
f[i][j] = max(f[i][j], f[last[x-j]][j]+1)
}
if x+j <= mx && last[x+j] >= 0 {
f[i][j] = max(f[i][j], f[last[x+j]][j]+1)
}
ans = max(ans, f[i][j])
}
last[x] = i
}
return
}
# Accepted solution for LeetCode #3409: Longest Subsequence With Decreasing Adjacent Difference
class Solution:
def longestSubsequence(self, nums: list[int]) -> int:
mx = max(nums)
# dp[num][diff] := the length of the longest subsequence ending in `num`
# s.t. the last absolute difference between consecutive elements is `diff`
dp = [[0] * (mx + 1) for _ in range(mx + 1)]
for num in nums:
for prev in range(1, mx + 1):
diff = abs(num - prev)
dp[num][diff] = max(dp[num][diff], dp[prev][diff] + 1)
# dp[num][diff] := max(dp[num][j]) for j >= diff
for j in range(mx - 1, -1, -1):
dp[num][j] = max(dp[num][j], dp[num][j + 1])
return max(map(max, dp))
// Accepted solution for LeetCode #3409: Longest Subsequence With Decreasing Adjacent Difference
// 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 #3409: Longest Subsequence With Decreasing Adjacent Difference
// class Solution {
// public int longestSubsequence(int[] nums) {
// final int mx = Arrays.stream(nums).max().getAsInt();
// // dp[num][diff] := the length of the longest subsequence ending in `num`
// // s.t. the last absolute difference between consecutive elements is `diff`
// int[][] dp = new int[mx + 1][mx + 1];
//
// for (final int num : nums) {
// for (int prev = 1; prev <= mx; ++prev) {
// final int diff = Math.abs(num - prev);
// dp[num][diff] = Math.max(dp[num][diff], dp[prev][diff] + 1);
// }
// // dp[num][diff] := max(dp[num][j]), where j >= diff
// for (int j = mx - 1; j >= 0; --j)
// dp[num][j] = Math.max(dp[num][j], dp[num][j + 1]);
// }
//
// return Arrays.stream(dp).mapToInt(row -> row[0]).max().getAsInt();
// }
// }
// Accepted solution for LeetCode #3409: Longest Subsequence With Decreasing Adjacent Difference
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3409: Longest Subsequence With Decreasing Adjacent Difference
// class Solution {
// public int longestSubsequence(int[] nums) {
// final int mx = Arrays.stream(nums).max().getAsInt();
// // dp[num][diff] := the length of the longest subsequence ending in `num`
// // s.t. the last absolute difference between consecutive elements is `diff`
// int[][] dp = new int[mx + 1][mx + 1];
//
// for (final int num : nums) {
// for (int prev = 1; prev <= mx; ++prev) {
// final int diff = Math.abs(num - prev);
// dp[num][diff] = Math.max(dp[num][diff], dp[prev][diff] + 1);
// }
// // dp[num][diff] := max(dp[num][j]), where j >= diff
// for (int j = mx - 1; j >= 0; --j)
// dp[num][j] = Math.max(dp[num][j], dp[num][j + 1]);
// }
//
// return Arrays.stream(dp).mapToInt(row -> row[0]).max().getAsInt();
// }
// }
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.