Welcome to Subscribe On Youtube

1695. Maximum Erasure Value

Description

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

Solutions

Solution 1: Array or Hash Table + Prefix Sum

We use an array or hash table $d$ to record the last occurrence of each number, use $s$ to record the prefix sum, and use $j$ to record the left endpoint of the current non-repeating subarray.

We traverse the array, for each number $v$, if $d[v]$ exists, then we update $j$ to $max(j, d[v])$, which ensures that the current non-repeating subarray does not contain $v$. Then we update the answer to $max(ans, s[i] - s[j])$, and finally update $d[v]$ to $i$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.

Solution 2: Two Pointers (Sliding Window)

The problem is essentially asking us to find the longest subarray where all elements are distinct. We can use two pointers $i$ and $j$ to point to the left and right boundaries of the subarray, initially $i = 0$ and $j = 0$. Additionally, we use a hash table $\text{vis}$ to record the elements in the subarray.

We iterate through the array. For each number $x$, if $x$ is in $\text{vis}$, we continuously remove $\text{nums}[i]$ from $\text{vis}$ until $x$ is no longer in $\text{vis}$. This way, we find a subarray that contains no duplicate elements. We add $x$ to $\text{vis}$, update the subarray sum $s$, and then update the answer $\text{ans} = \max(\text{ans}, s)$.

After the iteration, we can get the maximum subarray sum.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array $\text{nums}$.

  • 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;
        }
    }
    
    
    // Solution 2
    class Solution {
        public int maximumUniqueSubarray(int[] nums) {
            Set<Integer> vis = new HashSet<>();
            int ans = 0, s = 0, i = 0;
            for (int x : nums) {
                while (vis.contains(x)) {
                    s -= nums[i];
                    vis.remove(nums[i++]);
                }
                vis.add(x);
                s += x;
                ans = Math.max(ans, s);
            }
            return ans;
        }
    }
    
    
  • class Solution {
    public:
        int maximumUniqueSubarray(vector<int>& nums) {
            int d[10001]{};
            int n = nums.size();
            int s[n + 1];
            s[0] = 0;
            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 = max(j, d[v]);
                ans = max(ans, s[i] - s[j]);
                d[v] = i;
            }
            return ans;
        }
    };
    
    
    // Solution 2
    class Solution {
    public:
        int maximumUniqueSubarray(vector<int>& nums) {
            unordered_set<int> vis;
            int ans = 0, s = 0, i = 0;
            for (int x : nums) {
                while (vis.contains(x)) {
                    s -= nums[i];
                    vis.erase(nums[i++]);
                }
                vis.insert(x);
                s += x;
                ans = max(ans, s);
            }
            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
    
    
    # Solution 2
    class Solution:
        def maximumUniqueSubarray(self, nums: List[int]) -> int:
            vis = set()
            ans = s = i = 0
            for x in nums:
                while x in vis:
                    y = nums[i]
                    s -= y
                    vis.remove(y)
                    i += 1
                vis.add(x)
                s += x
                ans = max(ans, s)
            return ans
    
    
  • 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
    }
    
    
    // Solution 2
    func maximumUniqueSubarray(nums []int) (ans int) {
    	vis := map[int]bool{}
    	var s, i int
    	for _, x := range nums {
    		for vis[x] {
    			s -= nums[i]
    			vis[nums[i]] = false
    			i++
    		}
    		vis[x] = true
    		s += x
    		ans = max(ans, s)
    	}
    	return
    }
    
    
  • 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;
    }
    
    
    // Solution 2
    function maximumUniqueSubarray(nums: number[]): number {
        const vis: Set<number> = new Set();
        let [ans, s, i] = [0, 0, 0];
        for (const x of nums) {
            while (vis.has(x)) {
                s -= nums[i];
                vis.delete(nums[i++]);
            }
            vis.add(x);
            s += x;
            ans = Math.max(ans, s);
        }
        return ans;
    }
    
    
  • 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
        }
    }
    
    
    // Solution 2
    use std::collections::HashSet;
    
    impl Solution {
        pub fn maximum_unique_subarray(nums: Vec<i32>) -> i32 {
            let mut vis = HashSet::new();
            let (mut ans, mut s, mut i) = (0, 0, 0);
    
            for &x in &nums {
                while vis.contains(&x) {
                    let y = nums[i];
                    s -= y;
                    vis.remove(&y);
                    i += 1;
                }
                vis.insert(x);
                s += x;
                ans = ans.max(s);
            }
    
            ans
        }
    }
    
    

All Problems

All Solutions