You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements.
Return the maximum score you can get by erasing exactly one subarray.
An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r).
Example 1:
Input: nums = [4,2,4,5,6] Output: 17 Explanation: The optimal subarray here is [2,4,5,6].
Example 2:
Input: nums = [5,2,1,2,5,2,1,2,5] Output: 8 Explanation: The optimal subarray here is [5,2,1] or [1,2,5].
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the maximum erasure value involves exploring every single possible continuous segment of numbers. For each possible segment, we check if all the numbers inside it are completely unique before calculating their total sum.
Here's how the algorithm would work step-by-step:
def maximum_erasure_value(numbers_list):
highest_score_found = 0
# Explore every possible continuous segment by iterating over all start and end positions
for start_position in range(len(numbers_list)):
for end_position in range(start_position, len(numbers_list)):
current_segment = numbers_list[start_position:end_position + 1]
# Check if all numbers in the segment are unique to ensure it is a valid erasure
if len(current_segment) == len(set(current_segment)):
current_segment_sum = sum(current_segment)
# Update our running record if the current segment sum exceeds all previous sums
if current_segment_sum > highest_score_found:
highest_score_found = current_segment_sum
return highest_score_foundTo find the most valuable unique sequence, we can slide a flexible window across our collection of items, gathering them one by one. Whenever we notice we have collected a duplicate item, we simply shrink our window from the starting side until the duplicate is gone, ensuring every item in our current view is completely unique.
Here's how the algorithm would work step-by-step:
class Solution:
def maximum_unique_subarray(self, nums: list[int]) -> int:
seen_elements = set()
start_pointer = 0
current_window_sum = 0
maximum_total_score = 0
for end_pointer in range(len(nums)):
# We shrink the window from the left until all duplicates are removed to maintain uniqueness.
while nums[end_pointer] in seen_elements:
seen_elements.remove(nums[start_pointer])
current_window_sum -= nums[start_pointer]
start_pointer += 1
seen_elements.add(nums[end_pointer])
current_window_sum += nums[end_pointer]
# We update the maximum score only when we find a strictly better unique window total.
if current_window_sum > maximum_total_score:
maximum_total_score = current_window_sum
return maximum_total_score| Case | How to Handle |
|---|---|
| Empty or null array input | Return zero immediately because the score of an empty subarray is zero. |
| Array with a single element | The sliding window will process the single element and return its value as the maximum score. |
| Array with all identical elements | The left pointer will advance past the duplicate, restricting the valid window size to exactly one element. |
| Array with no duplicate elements | The sliding window will expand to encompass the entire array and return the sum of all elements. |
| Array with tightly clustered duplicates at the very end | The window correctly shrinks from the left to expel the duplicate while maintaining the peak running sum. |
| Maximum possible array size with large integer values | Using a linear time sliding window approach with a hash set and 64-bit integers prevents overflow and time-out issues. |
| Intermittent duplicate values scattered throughout the array | A frequency map or set tracks element indices to dynamically contract the window start whenever a repeat is encountered. |
| Maximum constraint values where the sum exceeds standard 32-bit integer limits | Using 64-bit integer types for sum accumulation ensures the result does not overflow during computation. |