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 two 0-indexed integer arrays nums1 and nums2, both of length n.
You can choose two integers left and right where 0 <= left <= right < n and swap the subarray nums1[left...right] with the subarray nums2[left...right].
nums1 = [1,2,3,4,5] and nums2 = [11,12,13,14,15] and you choose left = 1 and right = 2, nums1 becomes [1,12,13,4,5] and nums2 becomes [11,2,3,14,15].You may choose to apply the mentioned operation once or not do anything.
The score of the arrays is the maximum of sum(nums1) and sum(nums2), where sum(arr) is the sum of all the elements in the array arr.
Return the maximum possible score.
A subarray is a contiguous sequence of elements within an array. arr[left...right] denotes the subarray that contains the elements of nums between indices left and right (inclusive).
Example 1:
Input: nums1 = [60,60,60], nums2 = [10,90,10] Output: 210 Explanation: Choosing left = 1 and right = 1, we have nums1 = [60,90,60] and nums2 = [10,60,10]. The score is max(sum(nums1), sum(nums2)) = max(210, 80) = 210.
Example 2:
Input: nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20] Output: 220 Explanation: Choosing left = 3, right = 4, we have nums1 = [20,40,20,40,20] and nums2 = [50,20,50,70,30]. The score is max(sum(nums1), sum(nums2)) = max(140, 220) = 220.
Example 3:
Input: nums1 = [7,11,13], nums2 = [1,1,1] Output: 31 Explanation: We choose not to swap any subarray. The score is max(sum(nums1), sum(nums2)) = max(31, 3) = 31.
Constraints:
n == nums1.length == nums2.length1 <= n <= 1051 <= nums1[i], nums2[i] <= 104Problem summary: You are given two 0-indexed integer arrays nums1 and nums2, both of length n. You can choose two integers left and right where 0 <= left <= right < n and swap the subarray nums1[left...right] with the subarray nums2[left...right]. For example, if nums1 = [1,2,3,4,5] and nums2 = [11,12,13,14,15] and you choose left = 1 and right = 2, nums1 becomes [1,12,13,4,5] and nums2 becomes [11,2,3,14,15]. You may choose to apply the mentioned operation once or not do anything. The score of the arrays is the maximum of sum(nums1) and sum(nums2), where sum(arr) is the sum of all the elements in the array arr. Return the maximum possible score. A subarray is a contiguous sequence of elements within an array. arr[left...right] denotes the subarray that contains the elements of nums between indices left and right (inclusive).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[60,60,60] [10,90,10]
[20,40,20,70,30] [50,20,50,40,20]
[7,11,13] [1,1,1]
maximum-subarray)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2321: Maximum Score Of Spliced Array
class Solution {
public int maximumsSplicedArray(int[] nums1, int[] nums2) {
int s1 = 0, s2 = 0, n = nums1.length;
for (int i = 0; i < n; ++i) {
s1 += nums1[i];
s2 += nums2[i];
}
return Math.max(s2 + f(nums1, nums2), s1 + f(nums2, nums1));
}
private int f(int[] nums1, int[] nums2) {
int t = nums1[0] - nums2[0];
int mx = t;
for (int i = 1; i < nums1.length; ++i) {
int v = nums1[i] - nums2[i];
if (t > 0) {
t += v;
} else {
t = v;
}
mx = Math.max(mx, t);
}
return mx;
}
}
// Accepted solution for LeetCode #2321: Maximum Score Of Spliced Array
func maximumsSplicedArray(nums1 []int, nums2 []int) int {
s1, s2 := 0, 0
n := len(nums1)
for i, v := range nums1 {
s1 += v
s2 += nums2[i]
}
f := func(nums1, nums2 []int) int {
t := nums1[0] - nums2[0]
mx := t
for i := 1; i < n; i++ {
v := nums1[i] - nums2[i]
if t > 0 {
t += v
} else {
t = v
}
mx = max(mx, t)
}
return mx
}
return max(s2+f(nums1, nums2), s1+f(nums2, nums1))
}
# Accepted solution for LeetCode #2321: Maximum Score Of Spliced Array
class Solution:
def maximumsSplicedArray(self, nums1: List[int], nums2: List[int]) -> int:
def f(nums1, nums2):
d = [a - b for a, b in zip(nums1, nums2)]
t = mx = d[0]
for v in d[1:]:
if t > 0:
t += v
else:
t = v
mx = max(mx, t)
return mx
s1, s2 = sum(nums1), sum(nums2)
return max(s2 + f(nums1, nums2), s1 + f(nums2, nums1))
// Accepted solution for LeetCode #2321: Maximum Score Of Spliced Array
/**
* [2321] Maximum Score Of Spliced Array
*
* You are given two 0-indexed integer arrays nums1 and nums2, both of length n.
* You can choose two integers left and right where 0 <= left <= right < n and swap the subarray nums1[left...right] with the subarray nums2[left...right].
*
* For example, if nums1 = [1,2,3,4,5] and nums2 = [11,12,13,14,15] and you choose left = 1 and right = 2, nums1 becomes [1,<u>12,13</u>,4,5] and nums2 becomes [11,<u>2,3</u>,14,15].
*
* You may choose to apply the mentioned operation once or not do anything.
* The score of the arrays is the maximum of sum(nums1) and sum(nums2), where sum(arr) is the sum of all the elements in the array arr.
* Return the maximum possible score.
* A subarray is a contiguous sequence of elements within an array. arr[left...right] denotes the subarray that contains the elements of nums between indices left and right (inclusive).
*
* Example 1:
*
* Input: nums1 = [60,60,60], nums2 = [10,90,10]
* Output: 210
* Explanation: Choosing left = 1 and right = 1, we have nums1 = [60,<u>90</u>,60] and nums2 = [10,<u>60</u>,10].
* The score is max(sum(nums1), sum(nums2)) = max(210, 80) = 210.
* Example 2:
*
* Input: nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20]
* Output: 220
* Explanation: Choosing left = 3, right = 4, we have nums1 = [20,40,20,<u>40,20</u>] and nums2 = [50,20,50,<u>70,30</u>].
* The score is max(sum(nums1), sum(nums2)) = max(140, 220) = 220.
*
* Example 3:
*
* Input: nums1 = [7,11,13], nums2 = [1,1,1]
* Output: 31
* Explanation: We choose not to swap any subarray.
* The score is max(sum(nums1), sum(nums2)) = max(31, 3) = 31.
*
*
* Constraints:
*
* n == nums1.length == nums2.length
* 1 <= n <= 10^5
* 1 <= nums1[i], nums2[i] <= 10^4
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-score-of-spliced-array/
// discuss: https://leetcode.com/problems/maximum-score-of-spliced-array/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximums_spliced_array(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2321_example_1() {
let nums1 = vec![60, 60, 60];
let nums2 = vec![10, 90, 10];
let result = 210;
assert_eq!(Solution::maximums_spliced_array(nums1, nums2), result);
}
#[test]
#[ignore]
fn test_2321_example_2() {
let nums1 = vec![7, 11, 13];
let nums2 = vec![1, 1, 1];
let result = 31;
assert_eq!(Solution::maximums_spliced_array(nums1, nums2), result);
}
}
// Accepted solution for LeetCode #2321: Maximum Score Of Spliced Array
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2321: Maximum Score Of Spliced Array
// class Solution {
// public int maximumsSplicedArray(int[] nums1, int[] nums2) {
// int s1 = 0, s2 = 0, n = nums1.length;
// for (int i = 0; i < n; ++i) {
// s1 += nums1[i];
// s2 += nums2[i];
// }
// return Math.max(s2 + f(nums1, nums2), s1 + f(nums2, nums1));
// }
//
// private int f(int[] nums1, int[] nums2) {
// int t = nums1[0] - nums2[0];
// int mx = t;
// for (int i = 1; i < nums1.length; ++i) {
// int v = nums1[i] - nums2[i];
// if (t > 0) {
// t += v;
// } else {
// t = v;
// }
// mx = Math.max(mx, t);
// }
// return mx;
// }
// }
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.