##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1695.html

# 1695. Maximum Erasure Value (Medium)

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 <= 105
• 1 <= nums[i] <= 104

Related Topics:
Two Pointers

Similar Questions:

## Solution 1.

• class Solution {
public int maximumUniqueSubarray(int[] nums) {
int maxSum = 0;
int sum = 0;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int length = nums.length;
int start = 0, end = 0;
while (end < length) {
int num = nums[end++];
sum += num;
int count = map.getOrDefault(num, 0) + 1;
map.put(num, count);
while (map.size() < end - start) {
int prevNum = nums[start++];
sum -= prevNum;
int prevCount = map.get(prevNum) - 1;
if (prevCount > 0)
map.put(prevNum, prevCount);
else
map.remove(prevNum);
}
maxSum = Math.max(maxSum, sum);
}
return maxSum;
}
}

############

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;
}
}

• // OJ: https://leetcode.com/problems/maximum-erasure-value/
// Time: O(N)
// Space: O(N)
class Solution {
public:
int maximumUniqueSubarray(vector<int>& A) {
int i = 0, ans = 0, N = A.size(); // window [i, j] is a window which only contains unique elements.
unordered_map<int, int> m; // number -> index of last occurrence.
vector<int> sum(N + 1);
partial_sum(begin(A), end(A), begin(sum) + 1);
for (int j = 0; j < N; ++j) {
if (m.count(A[j])) i = max(i, m[A[j]] + 1);
m[A[j]] = j;
ans = max(ans, sum[j + 1] - sum[i]);
}
return ans;
}
};

• class Solution:
def maximumUniqueSubarray(self, nums: List[int]) -> int:
d = defaultdict(int)
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

############

# 1695. Maximum Erasure Value
# https://leetcode.com/problems/maximum-erasure-value/

class Solution:
def maximumUniqueSubarray(self, nums: List[int]) -> int:

c = collections.Counter()
j = curr = res = 0

for i,x in enumerate(nums):
curr += x
c[x] += 1

if c[x] == 2:
while c[x] > 1:
curr -= nums[j]
c[nums[j]] -= 1
j += 1

res = max(res, curr)

return res


• 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
}

func max(a, b int) int {
if a > b {
return a
}
return b
}