πΉ 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
β±οΈ 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)