DEV Community

Md. Rishat Talukder
Md. Rishat Talukder

Posted on

🧠 Solving LeetCode Until I Become Top 1% β€” Day `51`

πŸ”Ή Problem: 1695. Maximum Erasure Value

Difficulty: #Medium
Tags: #Array, #HashSet, #SlidingWindow


πŸ“ Problem Summary

You're given an array of positive integers.
You need to select a subarray (contiguous elements) that contains only unique elements and has the maximum possible sum.

Return the maximum sum of such a subarray.


🧠 My Thought Process

  • Brute Force Idea:

    • Try all subarrays and calculate the sum if all elements are unique.
    • Time Complexity: O(nΒ²) β€” definitely too slow for large inputs.
  • Optimized Strategy:

    • Use a sliding window to keep track of a window with unique elements.
    • Use a set dup to quickly check for duplicates.
    • Maintain a running total of current window sum.
    • If a duplicate is found, shrink the window from the left until the duplicate is removed.
  • Algorithm Used:

    βœ… [[Sliding_Window]]

    βœ… [[Hash_Set]]


βš™οΈ Code Implementation (Python)

class Solution:
    def maximumUniqueSubarray(self, nums: List[int]) -> int:
        res = 0
        left = 0
        dup = set()
        total = 0

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

            total += nums[right]
            dup.add(nums[right])
            res = max(res, total)

        return res
Enter fullscreen mode Exit fullscreen mode

⏱️ Time & Space Complexity

  • Time: O(n) β€” each element is added and removed from the set at most once.
  • Space: O(n) β€” in the worst case, the set could contain all n elements (if they're all unique).

🧩 Key Takeaways

  • βœ… Learned how to use the sliding window technique to maintain a window of unique elements.
  • πŸ’‘ The trick was realizing we could β€œslide” away duplicates by subtracting from the total sum dynamically.
  • πŸ’­ If I ever need to find the max/min of a property over a window with unique values β€” sliding window + set is a go-to.

πŸ” Reflection (Self-Check)

  • [x] Could I solve this without help?
  • [x] Did I write code from scratch?
  • [x] Did I understand why it works?
  • [x] Will I be able to recall this in a week?

πŸ“š Related Problems

  • [[3 Longest Substring Without Repeating Characters]]

πŸš€ Progress Tracker

Metric Value
Day 51
Total Problems Solved 392
Confidence Today πŸ˜ƒ
Leetcode Rating 1572

Top comments (0)