Skip to main content
  1. LeetCode/

LeetCode 1695: Maximum Erasure Value (Sliding Window)

·1 min· ·
LeetCode Daily Medium Array Sliding-Window Two-Pointers Hash-Set
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: Medium First Attempt: 2025-08-09

  • Total time: 00:00.00

Intuition
#

This is a classic sliding window with a uniqueness constraint. Maintain a window that contains no duplicates. Move the right pointer forward; while nums[right] is already in the window, shrink from the left by removing values and subtracting them from the running sum. Then insert nums[right], update the running sum, and track the maximum.

Approach
#

class Solution:
    def maximumUniqueSubarray(self, nums: List[int]) -> int:
        left = max_score = score_now = 0
        occur = set()

        for right in range(len(nums)):
            while nums[right] in occur:
                occur.remove(nums[left])
                score_now -= nums[left]
                left += 1

            occur.add(nums[right])
            score_now += nums[right]
            max_score = max(max_score, score_now)

        return max_score

Complexity
#

  • Time: O(n)
  • Space: O(n)

Findings
#

  • Keeping a running sum avoids recomputing subarray sums when the window moves.
  • A set gives O(1) membership checks to enforce uniqueness.
  • This is a standard two-pointer pattern; the same template applies to many “longest/maximum subarray with constraint” problems.

Encountered Problems
#

Related

LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 3439: Reschedule Meetings for Maximum Free Time (Sliding Window)
·3 mins
LeetCode Daily Medium Array Sliding-Window Two-Pointers Intervals
Sliding window over meetings; track total duration in a k-sized window and compute free span between boundaries.
LeetCode 1004: Max Consecutive Ones III
·3 mins
LeetCode Daily Medium Array Sliding-Window Two-Pointers Binary-Array
LeetCode Problem Solving - Variable Size Sliding Window with Zero Counting
LeetCode 904: Fruit Into Baskets
·3 mins
LeetCode Daily Medium Sliding-Window Variable-Window Two-Pointers Hash-Map
LeetCode Problem Solving - Variable Size Sliding Window with Hash Map
LeetCode 1052: Grumpy Bookstore Owner
·3 mins
LeetCode Daily Medium Array Sliding-Window Fixed-Window Optimization
LeetCode Problem Solving - Fixed Window with Customer Satisfaction Optimization
LeetCode 1658: Minimum Operations to Reduce X to Zero
·3 mins
LeetCode Daily Medium Array Sliding-Window Prefix-Sum Reverse-Thinking
LeetCode Problem Solving - Reverse Thinking with Sliding Window