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.

// OJ: https://leetcode.com/problems/maximum-erasure-value/
// Time: O(N)
// Space: O(N)
class Solution {
public:
    int maximumUniqueSubarray(vector<int>& A) {
        int i = -1, ans = 0, N = A.size();
        unordered_map<int, int> m;
        vector<int> sum(N + 1);
        for (int k = 0; k < N; ++k) sum[k + 1] = sum[k] + A[k];
        for (int j = 0; j < N; ++j) {
            if (m.count(A[j])) i = max(i, m[A[j]]);
            m[A[j]] = j;
            ans = max(ans, sum[j + 1] - sum[i + 1]);
        }
        return ans;
    }
};

Solution 2.

// OJ: https://leetcode.com/problems/maximum-erasure-value/
// Time: O(N)
// Space: O(N)
class Solution {
public:
    int maximumUniqueSubarray(vector<int>& A) {
        int ans = 0, N = A.size(), sum = 0;
        unordered_set<int> s;
        for (int i = 0, j = 0; j < N; ++j) {
            while (s.count(A[j])) {
                s.erase(A[i]);
                sum -= A[i++];
            }
            s.insert(A[j]);
            sum += A[j];
            ans = max(ans, sum);
        }
        return ans;
    }
};

Java

  • 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;
        }
    }
    
  • // 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;
        }
    };
    
  • # 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
                
            
    

All Problems

All Solutions