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 integer array nums of length n.
Choose an index i such that 0 <= i < n - 1.
For a chosen split index i:
prefixSum(i) be the sum of nums[0] + nums[1] + ... + nums[i].suffixMin(i) be the minimum value among nums[i + 1], nums[i + 2], ..., nums[n - 1].The score of a split at index i is defined as:
score(i) = prefixSum(i) - suffixMin(i)
Return an integer denoting the maximum score over all valid split indices.
Example 1:
Input: nums = [10,-1,3,-4,-5]
Output: 17
Explanation:
The optimal split is at i = 2, score(2) = prefixSum(2) - suffixMin(2) = (10 + (-1) + 3) - (-5) = 17.
Example 2:
Input: nums = [-7,-5,3]
Output: -2
Explanation:
The optimal split is at i = 0, score(0) = prefixSum(0) - suffixMin(0) = (-7) - (-5) = -2.
Example 3:
Input: nums = [1,1]
Output: 0
Explanation:
The only valid split is at i = 0, score(0) = prefixSum(0) - suffixMin(0) = 1 - 1 = 0.
Constraints:
2 <= nums.length <= 105-109 <= nums[i] <= 109Problem summary: You are given an integer array nums of length n. Choose an index i such that 0 <= i < n - 1. For a chosen split index i: Let prefixSum(i) be the sum of nums[0] + nums[1] + ... + nums[i]. Let suffixMin(i) be the minimum value among nums[i + 1], nums[i + 2], ..., nums[n - 1]. The score of a split at index i is defined as: score(i) = prefixSum(i) - suffixMin(i) Return an integer denoting the maximum score over all valid split indices.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[10,-1,3,-4,-5]
[-7,-5,3]
[1,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3788: Maximum Score of a Split
class Solution {
public long maximumScore(int[] nums) {
int n = nums.length;
long[] suf = new long[n];
suf[n - 1] = nums[n - 1];
for (int i = n - 2; i >= 0; --i) {
suf[i] = Math.min(nums[i], suf[i + 1]);
}
long ans = Long.MIN_VALUE;
long pre = 0;
for (int i = 0; i < n - 1; ++i) {
pre += nums[i];
ans = Math.max(ans, pre - suf[i + 1]);
}
return ans;
}
}
// Accepted solution for LeetCode #3788: Maximum Score of a Split
func maximumScore(nums []int) int64 {
n := len(nums)
suf := make([]int64, n)
suf[n-1] = int64(nums[n-1])
for i := n - 2; i >= 0; i-- {
suf[i] = min(int64(nums[i]), suf[i+1])
}
var pre int64 = 0
var ans int64 = math.MinInt64
for i := 0; i < n-1; i++ {
pre += int64(nums[i])
ans = max(ans, pre-suf[i+1])
}
return ans
}
# Accepted solution for LeetCode #3788: Maximum Score of a Split
class Solution:
def maximumScore(self, nums: List[int]) -> int:
n = len(nums)
suf = [nums[-1]] * n
for i in range(n - 2, -1, -1):
suf[i] = min(nums[i], suf[i + 1])
ans = -inf
pre = 0
for i in range(n - 1):
pre += nums[i]
ans = max(ans, pre - suf[i + 1])
return ans
// Accepted solution for LeetCode #3788: Maximum Score of a Split
// 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 #3788: Maximum Score of a Split
// class Solution {
// public long maximumScore(int[] nums) {
// int n = nums.length;
// long[] suf = new long[n];
// suf[n - 1] = nums[n - 1];
// for (int i = n - 2; i >= 0; --i) {
// suf[i] = Math.min(nums[i], suf[i + 1]);
// }
// long ans = Long.MIN_VALUE;
// long pre = 0;
// for (int i = 0; i < n - 1; ++i) {
// pre += nums[i];
// ans = Math.max(ans, pre - suf[i + 1]);
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3788: Maximum Score of a Split
function maximumScore(nums: number[]): number {
const n = nums.length;
const suf: number[] = new Array(n);
suf[n - 1] = nums[n - 1];
for (let i = n - 2; i >= 0; --i) {
suf[i] = Math.min(nums[i], suf[i + 1]);
}
let ans = Number.NEGATIVE_INFINITY;
let pre = 0;
for (let i = 0; i < n - 1; ++i) {
pre += nums[i];
ans = Math.max(ans, pre - suf[i + 1]);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.