Skip to content
LC-1695 Medium LeetCode

1695. Maximum Erasure Value

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 59% Topics: Array, Hash Table, Sliding Window
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(n)

class Solution(object):
    def maximumUniqueSubarray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        lookup = {}
        prefix = [0]*(len(nums)+1)
        result, left = 0, 0
        for right, num in enumerate(nums):
            prefix[right+1] = prefix[right]+num
            if num in lookup:
                left = max(left, lookup[num]+1)
            lookup[num] = right
            result = max(result, prefix[right+1]-prefix[left])
        return result

Solution from kamyu104/LeetCode-Solutions · MIT