You are given an integer array nums
.
You are allowed to delete any number of elements from nums
without making it empty. After performing the deletions, select a subarray of nums
such that:
Return the maximum sum of such a subarray.
Example 1:
Input: nums = [1,2,3,4,5]
Output: 15
Explanation:
Select the entire array without deleting any element to obtain the maximum sum.
Example 2:
Input: nums = [1,1,0,1,1]
Output: 1
Explanation:
Delete the element nums[0] == 1
, nums[1] == 1
, nums[2] == 0
, and nums[3] == 1
. Select the entire array [1]
to obtain the maximum sum.
Example 3:
Input: nums = [1,2,-1,-2,1,0,-1]
Output: 3
Explanation:
Delete the elements nums[2] == -1
and nums[3] == -2
, and select the subarray [2, 1]
from [1, 2, 1, 0, -1]
to obtain the maximum sum.
Constraints:
1 <= nums.length <= 100
-100 <= nums[i] <= 100
When 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 this problem is like trying out every possible piece of a larger sequence. We look at every consecutive section to see if it fits our needs, in this case finding the section with unique numbers that also adds up to the biggest sum.
Here's how the algorithm would work step-by-step:
def maximum_unique_subarray(numbers):
maximum_sum = 0
for start_index in range(len(numbers)):
current_sum = 0
seen_numbers = set()
for end_index in range(start_index, len(numbers)):
current_number = numbers[end_index]
# If we've seen the number before, it's not unique, so break
if current_number in seen_numbers:
break
seen_numbers.add(current_number)
current_sum += current_number
# Update the maximum sum if we find a larger subarray sum
if current_sum > maximum_sum:
maximum_sum = current_sum
return maximum_sum
The key is to efficiently track a 'window' of numbers. We slide this window across the input, expanding it to include more numbers and shrinking it to remove numbers, always making sure that the numbers within the window are unique. By doing this, we can find the window with the maximum sum without checking every single possible subarray.
Here's how the algorithm would work step-by-step:
def maximum_unique_subarray(
number_list
):
window_start = 0
window_sum = 0
maximum_sum = 0
seen_numbers = set()
for window_end in range(len(number_list)):
while number_list[window_end] in seen_numbers:
# Shrink window if duplicate is found
window_sum -= number_list[window_start]
seen_numbers.remove(number_list[window_start])
window_start += 1
window_sum += number_list[window_end]
seen_numbers.add(number_list[window_end])
# Update maximum sum with each window update.
maximum_sum = max(maximum_sum, window_sum)
return maximum_sum
Case | How to Handle |
---|---|
Null or empty input array | Return 0 if the input array is null or empty, as there are no elements to sum. |
Input array with only one element | Return the value of the single element if it's unique (which it will always be), since removing nothing is not allowed. |
Array with all identical values | Calculate the sum of the unique element (the repeated value) for the entire array. |
Array with all unique values | Iterate through all possible subarray removals and choose the option that results in largest sum of unique elements in the remaining array. |
Large input array with a small number of unique elements | The sliding window approach should still be efficient as the size of the unique element set is limited and the rest is not relevant. |
Integer overflow when calculating sums | Use `long` data type to store sums to prevent integer overflow for very large input numbers and large input size. |
Array with extreme values (e.g., very large positive integers) | The solution should correctly handle large positive integers within the limits of the chosen data type (long). |
Removing the first or last element | The algorithm needs to correctly consider cases where the removed subarray starts at index 0 or ends at index nums.length - 1. |