LeetCode 1695. Maximum Erasure Value Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
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 position of each number, and use a prefix sum array s to record the sum from the starting point to the current position. We use a variable j to record the left endpoint of the current non-repeating subarray.

We iterate through the array. For each number v, if d[v] exists, 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), where n is the length of the array nums.

PythonJavaC++GoTypeScriptRust
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(code-box)

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 vis to record the elements in the subarray.

We iterate through the array. For each number x, if x is in vis, we continuously remove nums[i] from vis until x is no longer in vis. This way, we find a subarray that contains no duplicate elements. We add x to vis, update the subarray sum s, and then update the answer ans = max(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 nums.

PythonJavaC++GoTypeScriptRust
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(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !