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 positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements.
Return the maximum score you can get by erasing exactly one subarray.
An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r).
Example 1:
Input: nums = [4,2,4,5,6] Output: 17 Explanation: The optimal subarray here is [2,4,5,6].
Example 2:
Input: nums = [5,2,1,2,5,2,1,2,5] Output: 8 Explanation: The optimal subarray here is [5,2,1] or [1,2,5].
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 104Problem summary: You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements. Return the maximum score you can get by erasing exactly one subarray. An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Sliding Window
[4,2,4,5,6]
[5,2,1,2,5,2,1,2,5]
longest-substring-without-repeating-characters)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1695: Maximum Erasure Value
class Solution {
public int maximumUniqueSubarray(int[] nums) {
int[] d = new int[10001];
int n = nums.length;
int[] s = new int[n + 1];
for (int i = 0; i < n; ++i) {
s[i + 1] = s[i] + nums[i];
}
int ans = 0, j = 0;
for (int i = 1; i <= n; ++i) {
int v = nums[i - 1];
j = Math.max(j, d[v]);
ans = Math.max(ans, s[i] - s[j]);
d[v] = i;
}
return ans;
}
}
// Accepted solution for LeetCode #1695: Maximum Erasure Value
func maximumUniqueSubarray(nums []int) (ans int) {
d := [10001]int{}
n := len(nums)
s := make([]int, n+1)
for i, v := range nums {
s[i+1] = s[i] + v
}
for i, j := 1, 0; i <= n; i++ {
v := nums[i-1]
j = max(j, d[v])
ans = max(ans, s[i]-s[j])
d[v] = i
}
return
}
# Accepted solution for LeetCode #1695: Maximum Erasure Value
class Solution:
def maximumUniqueSubarray(self, nums: List[int]) -> int:
d = [0] * (max(nums) + 1)
s = list(accumulate(nums, initial=0))
ans = j = 0
for i, v in enumerate(nums, 1):
j = max(j, d[v])
ans = max(ans, s[i] - s[j])
d[v] = i
return ans
// Accepted solution for LeetCode #1695: Maximum Erasure Value
impl Solution {
pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
let m = *nums.iter().max().unwrap() as usize;
let mut d = vec![0; m + 1];
let n = nums.len();
let mut s = vec![0; n + 1];
for i in 0..n {
s[i + 1] = s[i] + nums[i];
}
let mut ans = 0;
let mut j = 0;
for (i, &v) in nums.iter().enumerate().map(|(i, v)| (i + 1, v)) {
j = j.max(d[v as usize]);
ans = ans.max(s[i] - s[j]);
d[v as usize] = i;
}
ans
}
}
// Accepted solution for LeetCode #1695: Maximum Erasure Value
function maximumUniqueSubarray(nums: number[]): number {
const m = Math.max(...nums);
const n = nums.length;
const s: number[] = Array.from({ length: n + 1 }, () => 0);
for (let i = 1; i <= n; ++i) {
s[i] = s[i - 1] + nums[i - 1];
}
const d = Array.from({ length: m + 1 }, () => 0);
let [ans, j] = [0, 0];
for (let i = 1; i <= n; ++i) {
j = Math.max(j, d[nums[i - 1]]);
ans = Math.max(ans, s[i] - s[j]);
d[nums[i - 1]] = i;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each starting index, scan the next k elements to compute the window aggregate. There are n−k+1 starting positions, each requiring O(k) work, giving O(n × k) total. No extra space since we recompute from scratch each time.
The window expands and contracts as we scan left to right. Each element enters the window at most once and leaves at most once, giving 2n total operations = O(n). Space depends on what we track inside the window (a hash map of at most k distinct elements, or O(1) for a fixed-size window).
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Using `if` instead of `while` leaves the window invalid for multiple iterations.
Usually fails on: Over-limit windows stay invalid and produce wrong lengths/counts.
Fix: Shrink in a `while` loop until the invariant is valid again.