#80 ๐๐งฎ 2461. Maximum Sum of Distinct Subarrays With Length K ๐ง ๐
When we think about finding maximum subarray sums, things can get tricky when thereโs a twist: all elements in the subarray must be distinct! ๐คฏ This problem brings an exciting challenge, combining sliding window techniques and a hint of hash magic ๐ช to create a solution thatโs efficient and clean.
๐ก Problem Statement
You are given an integer array nums
and an integer k
. Find the maximum subarray sum of all the subarrays of nums
that meet the following conditions:
- The length of the subarray is
k
, and - All elements of the subarray are distinct.
If no subarray meets the conditions, return 0
.
โ๏ธ Definitions
- A subarray is a contiguous, non-empty sequence of elements within an array.
- A subarray must have exactly
k
elements to be considered valid. - If any element in a subarray repeats, the subarray is invalid.
๐ Examples
Example 1
Input: nums = [1, 5, 4, 2, 9, 9, 9], k = 3
Output: 15
Explanation:
The subarrays of nums
with length k
are:
[1, 5, 4]
โ Valid, sum =10
[5, 4, 2]
โ Valid, sum =11
[4, 2, 9]
โ Valid, sum =15
(maximum sum ๐)[2, 9, 9]
โ Invalid (element9
repeats)[9, 9, 9]
โ Invalid (element9
repeats)
Output: 15
Example 2
Input: nums = [4, 4, 4], k = 3
Output: 0
Explanation:
The only subarray of length k
is [4, 4, 4]
, which is invalid because element 4
repeats.
Output: 0
๐ Constraints
1 <= k <= nums.length <= 10โต
1 <= nums[i] <= 10โต
๐ถ Step-by-Step Plan
This problem is best solved using the sliding window technique with a hash set or hash map to efficiently track the distinct elements in the current window.
๐ ๏ธ Solution with Python (Optimized Single Solution)
Since both the base and optimized solutions share the same time complexity, weโll focus on the optimized sliding window approach.
๐ Algorithm
- Initialize a sliding window with size
k
and maintain:- A
current_sum
for the current windowโs sum. - A
max_sum
to store the maximum sum of all valid windows. - A
frequency_map
(orset
) to ensure all elements in the window are distinct.
- A
- Slide the window across the array:
- Add the new element at the end of the window to the
current_sum
. - Remove the element falling out of the window (if any) from the
current_sum
. - Use the
frequency_map
to check if all elements in the window are distinct.
- Add the new element at the end of the window to the
-
If a window is valid (all elements distinct), update the
max_sum
. - Return
max_sum
if any valid window exists, else return0
.
๐ Python Implementation
def maximumSubarraySum(nums, k):
from collections import defaultdict
# Sliding window variables
max_sum = 0
current_sum = 0
frequency_map = defaultdict(int)
left = 0 # Left pointer of the sliding window
for right in range(len(nums)):
# Add the right element into the window
frequency_map[nums[right]] += 1
current_sum += nums[right]
# If the window size exceeds k, shrink from the left
if right - left + 1 > k:
frequency_map[nums[left]] -= 1
if frequency_map[nums[left]] == 0:
del frequency_map[nums[left]]
current_sum -= nums[left]
left += 1
# Check if the current window is valid (distinct elements only)
if right - left + 1 == k and len(frequency_map) == k:
max_sum = max(max_sum, current_sum)
return max_sum
โฑ๏ธ Time Complexity
- Sliding Window Operations: Each element is added/removed from the
frequency_map
exactly once.
Complexity = (O(n)), where (n) is the length ofnums
. - Overall: (O(n)).
๐งต Space Complexity
- Space for the
frequency_map
: At most (O(k)), where (k) is the window size. - Overall: (O(k)).
๐ Walkthrough of Example
Input: nums = [1, 5, 4, 2, 9, 9, 9], k = 3
- Initial State:
max_sum = 0
current_sum = 0
frequency_map = {}
-
Sliding the Window:
- Window
[1, 5, 4]
:- Add
1, 5, 4
โ Valid โcurrent_sum = 10
โmax_sum = 10
.
- Add
- Window
[5, 4, 2]
:- Add
2
, remove1
โ Valid โcurrent_sum = 11
โmax_sum = 11
.
- Add
- Window
[4, 2, 9]
:- Add
9
, remove5
โ Valid โcurrent_sum = 15
โmax_sum = 15
.
- Add
- Window
[2, 9, 9]
:- Add
9
, remove4
โ Invalid (duplicate9
).
- Add
- Window
[9, 9, 9]
:- All elements invalid.
- Window
Output: 15
โ๏ธ Edge Cases
- Single Element Array:
nums = [7], k = 1 Output: 7
- All Elements Identical:
nums = [5, 5, 5], k = 2 Output: 0
- Subarray Length Larger Than Array Length:
nums = [1, 2], k = 3 Output: 0
- Large Array with All Unique Elements:
nums = [1, 2, 3, 4, ..., 10000], k = 5 Output: Sum of max 5 unique consecutive elements
๐ Conclusion
This problem beautifully demonstrates the power of the sliding window technique! ๐ช By efficiently tracking distinct elements, we solved the problem in (O(n)) time complexity, handling edge cases gracefully.
Happy Coding! ๐