Sliding Window
Max Sum of Distinct Subarrays Length k
DESCRIPTION (inspired by Leetcode.com)
Given an integer array nums and an integer k, write a function to identify the highest possible sum of a subarray within nums, where the subarray meets the following criteria: its length is k, and all of its elements are unique. If no such subarray exists, return 0.
Example 1: Input:
nums = [3, 2, 2, 3, 4, 6, 7, 7, -1] k = 4
Output:
20
Explanation: The subarrays of nums with length 4 are:
[3, 2, 2, 3] # elements 3 and 2 are repeated. [2, 2, 3, 4] # element 2 is repeated. [2, 3, 4, 6] # meets the requirements and has a sum of 15. [3, 4, 6, 7] # meets the requirements and has a sum of 20. [4, 6, 7, 7] # element 7 is repeated. [6, 7, 7, -1] # element 7 is repeated.
We return 20 because it is the maximum subarray sum of all the subarrays that meet the conditions.
Example 2: Input:
nums = [5, 5, 5, 5, 5] k = 3
Output:
0
Explanation: Every subarray of length 3 contains duplicate elements, so no valid subarray exists. Return 0.
Explanation
- curr_sum: The sum of all elements in the window.
- state: A dictionary mapping each element in the window to the number of times it appears in the window.
def maxSum(nums, k):max_sum = 0start = 0state = {}curr_sum = 0for end in range(len(nums)):curr_sum = curr_sum + nums[end]state[nums[end]] = state.get(nums[end], 0) + 1if end - start + 1 == k:if len(state) == k:max_sum = max(max_sum, curr_sum)curr_sum = curr_sum - nums[start]state[nums[start]] = state[nums[start]] - 1if state[nums[start]] == 0:del state[nums[start]]start += 1return max_sum
start
max sum distinct subarrays of size k
0 / 5
def maxSum(nums, k):max_sum = 0start = 0state = {}curr_sum = 0for end in range(len(nums)):curr_sum = curr_sum + nums[end]state[nums[end]] = state.get(nums[end], 0) + 1if end - start + 1 == k:if len(state) == k:max_sum = max(max_sum, curr_sum)curr_sum = curr_sum - nums[start]state[nums[start]] = state[nums[start]] - 1if state[nums[start]] == 0:del state[nums[start]]start += 1return max_sum
curr_sum: 10
start: 0 | end: 3
expand window
0 / 4
def maxSum(nums, k):max_sum = 0start = 0state = {}curr_sum = 0for end in range(len(nums)):curr_sum = curr_sum + nums[end]state[nums[end]] = state.get(nums[end], 0) + 1if end - start + 1 == k:if len(state) == k:max_sum = max(max_sum, curr_sum)curr_sum = curr_sum - nums[start]state[nums[start]] = state[nums[start]] - 1if state[nums[start]] == 0:del state[nums[start]]start += 1return max_sum
curr_sum: 15
start: 2 | end: 5
expand window
0 / 10
Example Input 2
- [5, 5, 5] - all three elements are the same (not distinct)
- [5, 5, 5] - still all duplicates
- [5, 5, 5] - same issue
Solution
def maxSum(nums, k):max_sum = 0start = 0state = {}curr_sum = 0for end in range(len(nums)):curr_sum = curr_sum + nums[end]state[nums[end]] = state.get(nums[end], 0) + 1if end - start + 1 == k:if len(state) == k:max_sum = max(max_sum, curr_sum)curr_sum = curr_sum - nums[start]state[nums[start]] = state[nums[start]] - 1if state[nums[start]] == 0:del state[nums[start]]start += 1return max_sum
start
max sum distinct subarrays of size k
0 / 19
Mark as read
Unlock Premium Coding Content
On This Page
Your account is free and you can post anonymously if you choose.