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 two 0-indexed integer arrays nums1 and nums2 of length n.
Let's define another 0-indexed integer array, nums3, of length n. For each index i in the range [0, n - 1], you can assign either nums1[i] or nums2[i] to nums3[i].
Your task is to maximize the length of the longest non-decreasing subarray in nums3 by choosing its values optimally.
Return an integer representing the length of the longest non-decreasing subarray in nums3.
Note: A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums1 = [2,3,1], nums2 = [1,2,1] Output: 2 Explanation: One way to construct nums3 is: nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1]. The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2. We can show that 2 is the maximum achievable length.
Example 2:
Input: nums1 = [1,3,2,1], nums2 = [2,2,3,4] Output: 4 Explanation: One way to construct nums3 is: nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4]. The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.
Example 3:
Input: nums1 = [1,1], nums2 = [2,2] Output: 2 Explanation: One way to construct nums3 is: nums3 = [nums1[0], nums1[1]] => [1,1]. The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.
Constraints:
1 <= nums1.length == nums2.length == n <= 1051 <= nums1[i], nums2[i] <= 109Problem summary: You are given two 0-indexed integer arrays nums1 and nums2 of length n. Let's define another 0-indexed integer array, nums3, of length n. For each index i in the range [0, n - 1], you can assign either nums1[i] or nums2[i] to nums3[i]. Your task is to maximize the length of the longest non-decreasing subarray in nums3 by choosing its values optimally. Return an integer representing the length of the longest non-decreasing subarray in nums3. Note: A subarray is a contiguous non-empty sequence of elements within an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[2,3,1] [1,2,1]
[1,3,2,1] [2,2,3,4]
[1,1] [2,2]
russian-doll-envelopes)maximum-length-of-pair-chain)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2771: Longest Non-decreasing Subarray From Two Arrays
class Solution {
public int maxNonDecreasingLength(int[] nums1, int[] nums2) {
int n = nums1.length;
int f = 1, g = 1;
int ans = 1;
for (int i = 1; i < n; ++i) {
int ff = 1, gg = 1;
if (nums1[i] >= nums1[i - 1]) {
ff = Math.max(ff, f + 1);
}
if (nums1[i] >= nums2[i - 1]) {
ff = Math.max(ff, g + 1);
}
if (nums2[i] >= nums1[i - 1]) {
gg = Math.max(gg, f + 1);
}
if (nums2[i] >= nums2[i - 1]) {
gg = Math.max(gg, g + 1);
}
f = ff;
g = gg;
ans = Math.max(ans, Math.max(f, g));
}
return ans;
}
}
// Accepted solution for LeetCode #2771: Longest Non-decreasing Subarray From Two Arrays
func maxNonDecreasingLength(nums1 []int, nums2 []int) int {
n := len(nums1)
f, g, ans := 1, 1, 1
for i := 1; i < n; i++ {
ff, gg := 1, 1
if nums1[i] >= nums1[i-1] {
ff = max(ff, f+1)
}
if nums1[i] >= nums2[i-1] {
ff = max(ff, g+1)
}
if nums2[i] >= nums1[i-1] {
gg = max(gg, f+1)
}
if nums2[i] >= nums2[i-1] {
gg = max(gg, g+1)
}
f, g = ff, gg
ans = max(ans, max(f, g))
}
return ans
}
# Accepted solution for LeetCode #2771: Longest Non-decreasing Subarray From Two Arrays
class Solution:
def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
f = g = 1
ans = 1
for i in range(1, n):
ff = gg = 1
if nums1[i] >= nums1[i - 1]:
ff = max(ff, f + 1)
if nums1[i] >= nums2[i - 1]:
ff = max(ff, g + 1)
if nums2[i] >= nums1[i - 1]:
gg = max(gg, f + 1)
if nums2[i] >= nums2[i - 1]:
gg = max(gg, g + 1)
f, g = ff, gg
ans = max(ans, f, g)
return ans
// Accepted solution for LeetCode #2771: Longest Non-decreasing Subarray From Two Arrays
// 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 #2771: Longest Non-decreasing Subarray From Two Arrays
// class Solution {
// public int maxNonDecreasingLength(int[] nums1, int[] nums2) {
// int n = nums1.length;
// int f = 1, g = 1;
// int ans = 1;
// for (int i = 1; i < n; ++i) {
// int ff = 1, gg = 1;
// if (nums1[i] >= nums1[i - 1]) {
// ff = Math.max(ff, f + 1);
// }
// if (nums1[i] >= nums2[i - 1]) {
// ff = Math.max(ff, g + 1);
// }
// if (nums2[i] >= nums1[i - 1]) {
// gg = Math.max(gg, f + 1);
// }
// if (nums2[i] >= nums2[i - 1]) {
// gg = Math.max(gg, g + 1);
// }
// f = ff;
// g = gg;
// ans = Math.max(ans, Math.max(f, g));
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2771: Longest Non-decreasing Subarray From Two Arrays
function maxNonDecreasingLength(nums1: number[], nums2: number[]): number {
const n = nums1.length;
let [f, g, ans] = [1, 1, 1];
for (let i = 1; i < n; ++i) {
let [ff, gg] = [1, 1];
if (nums1[i] >= nums1[i - 1]) {
ff = Math.max(ff, f + 1);
}
if (nums1[i] >= nums2[i - 1]) {
ff = Math.max(ff, g + 1);
}
if (nums2[i] >= nums1[i - 1]) {
gg = Math.max(gg, f + 1);
}
if (nums2[i] >= nums2[i - 1]) {
gg = Math.max(gg, g + 1);
}
f = ff;
g = gg;
ans = Math.max(ans, f, g);
}
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.