Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created July 22, 2025 19:50
Show Gist options
  • Save Ifihan/2e695ef813490b2136cc064083538b04 to your computer and use it in GitHub Desktop.
Save Ifihan/2e695ef813490b2136cc064083538b04 to your computer and use it in GitHub Desktop.
Maximum Erasure Value

Question

Approach

I used a sliding window technique with a set to keep track of the unique elements in the current window using two pointers: left and right. As I moved right through the array, I kept adding elements to a running sum and to the set. If I encountered a duplicate element (i.e., one already in the set), I slid the left pointer forward and removed elements from the set and subtracted their values from the sum until the duplicate was removed. At each step, I updated the maximum sum I could get.

Implementation

class Solution:
    def maximumUniqueSubarray(self, nums: List[int]) -> int:
        seen = set()
        left = 0
        current_sum = 0
        max_sum = 0

        for right in range(len(nums)):
            while nums[right] in seen:
                seen.remove(nums[left])
                current_sum -= nums[left]
                left += 1
            seen.add(nums[right])
            current_sum += nums[right]
            max_sum = max(max_sum, current_sum)

        return max_sum

Complexities

  • Time: O(n)
  • Space: O(n)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment