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 integer array nums. You have to partition the array into one or more contiguous subarrays.
We call a partition of the array valid if each of the obtained subarrays satisfies one of the following conditions:
2, equal elements. For example, the subarray [2,2] is good.3, equal elements. For example, the subarray [4,4,4] is good.3 consecutive increasing elements, that is, the difference between adjacent elements is 1. For example, the subarray [3,4,5] is good, but the subarray [1,3,5] is not.Return true if the array has at least one valid partition. Otherwise, return false.
Example 1:
Input: nums = [4,4,4,5,6] Output: true Explanation: The array can be partitioned into the subarrays [4,4] and [4,5,6]. This partition is valid, so we return true.
Example 2:
Input: nums = [1,1,1,2] Output: false Explanation: There is no valid partition for this array.
Constraints:
2 <= nums.length <= 1051 <= nums[i] <= 106Problem summary: You are given a 0-indexed integer array nums. You have to partition the array into one or more contiguous subarrays. We call a partition of the array valid if each of the obtained subarrays satisfies one of the following conditions: The subarray consists of exactly 2, equal elements. For example, the subarray [2,2] is good. The subarray consists of exactly 3, equal elements. For example, the subarray [4,4,4] is good. The subarray consists of exactly 3 consecutive increasing elements, that is, the difference between adjacent elements is 1. For example, the subarray [3,4,5] is good, but the subarray [1,3,5] is not. Return true if the array has at least one valid partition. Otherwise, return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[4,4,4,5,6]
[1,1,1,2]
count-the-number-of-good-partitions)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2369: Check if There is a Valid Partition For The Array
class Solution {
private int n;
private int[] nums;
private Boolean[] f;
public boolean validPartition(int[] nums) {
n = nums.length;
this.nums = nums;
f = new Boolean[n];
return dfs(0);
}
private boolean dfs(int i) {
if (i >= n) {
return true;
}
if (f[i] != null) {
return f[i];
}
boolean a = i + 1 < n && nums[i] == nums[i + 1];
boolean b = i + 2 < n && nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2];
boolean c = i + 2 < n && nums[i + 1] - nums[i] == 1 && nums[i + 2] - nums[i + 1] == 1;
return f[i] = ((a && dfs(i + 2)) || ((b || c) && dfs(i + 3)));
}
}
// Accepted solution for LeetCode #2369: Check if There is a Valid Partition For The Array
func validPartition(nums []int) bool {
n := len(nums)
f := make([]int, n)
for i := range f {
f[i] = -1
}
var dfs func(int) bool
dfs = func(i int) bool {
if i == n {
return true
}
if f[i] != -1 {
return f[i] == 1
}
a := i+1 < n && nums[i] == nums[i+1]
b := i+2 < n && nums[i] == nums[i+1] && nums[i+1] == nums[i+2]
c := i+2 < n && nums[i+1]-nums[i] == 1 && nums[i+2]-nums[i+1] == 1
f[i] = 0
if a && dfs(i+2) || b && dfs(i+3) || c && dfs(i+3) {
f[i] = 1
}
return f[i] == 1
}
return dfs(0)
}
# Accepted solution for LeetCode #2369: Check if There is a Valid Partition For The Array
class Solution:
def validPartition(self, nums: List[int]) -> bool:
@cache
def dfs(i: int) -> bool:
if i >= n:
return True
a = i + 1 < n and nums[i] == nums[i + 1]
b = i + 2 < n and nums[i] == nums[i + 1] == nums[i + 2]
c = (
i + 2 < n
and nums[i + 1] - nums[i] == 1
and nums[i + 2] - nums[i + 1] == 1
)
return (a and dfs(i + 2)) or ((b or c) and dfs(i + 3))
n = len(nums)
return dfs(0)
// Accepted solution for LeetCode #2369: Check if There is a Valid Partition For The Array
/**
* [2369] Check if There is a Valid Partition For The Array
*
* You are given a 0-indexed integer array nums. You have to partition the array into one or more contiguous subarrays.
* We call a partition of the array valid if each of the obtained subarrays satisfies one of the following conditions:
* <ol>
* The subarray consists of exactly 2, equal elements. For example, the subarray [2,2] is good.
* The subarray consists of exactly 3, equal elements. For example, the subarray [4,4,4] is good.
* The subarray consists of exactly 3 consecutive increasing elements, that is, the difference between adjacent elements is 1. For example, the subarray [3,4,5] is good, but the subarray [1,3,5] is not.
* </ol>
* Return true if the array has at least one valid partition. Otherwise, return false.
*
* Example 1:
*
* Input: nums = [4,4,4,5,6]
* Output: true
* Explanation: The array can be partitioned into the subarrays [4,4] and [4,5,6].
* This partition is valid, so we return true.
*
* Example 2:
*
* Input: nums = [1,1,1,2]
* Output: false
* Explanation: There is no valid partition for this array.
*
*
* Constraints:
*
* 2 <= nums.length <= 10^5
* 1 <= nums[i] <= 10^6
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/
// discuss: https://leetcode.com/problems/check-if-there-is-a-valid-partition-for-the-array/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn valid_partition(nums: Vec<i32>) -> bool {
false
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2369_example_1() {
let nums = vec![4, 4, 4, 5, 6];
let result = true;
assert_eq!(Solution::valid_partition(nums), result);
}
#[test]
#[ignore]
fn test_2369_example_2() {
let nums = vec![1, 1, 1, 2];
let result = false;
assert_eq!(Solution::valid_partition(nums), result);
}
}
// Accepted solution for LeetCode #2369: Check if There is a Valid Partition For The Array
function validPartition(nums: number[]): boolean {
const n = nums.length;
const f: number[] = Array(n).fill(-1);
const dfs = (i: number): boolean => {
if (i >= n) {
return true;
}
if (f[i] !== -1) {
return f[i] === 1;
}
const a = i + 1 < n && nums[i] == nums[i + 1];
const b = i + 2 < n && nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2];
const c = i + 2 < n && nums[i + 1] - nums[i] == 1 && nums[i + 2] - nums[i + 1] == 1;
f[i] = (a && dfs(i + 2)) || ((b || c) && dfs(i + 3)) ? 1 : 0;
return f[i] == 1;
};
return dfs(0);
}
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.