2461. Maximum Sum of Distinct Subarrays With Length K

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 the elements of the subarray are distinct.

Return the maximum subarray sum of all the subarrays that meet the conditions. If no subarray meets the conditions, return 0.

A subarray is a contiguous non-empty sequence of elements within an array.

  Example 1:

Input: nums = [1,5,4,2,9,9,9], k = 3
Output: 15
Explanation: The subarrays of nums with length 3 are:
- [1,5,4] which meets the requirements and has a sum of 10.
- [5,4,2] which meets the requirements and has a sum of 11.
- [4,2,9] which meets the requirements and has a sum of 15.
- [2,9,9] which does not meet the requirements because the element 9 is repeated.
- [9,9,9] which does not meet the requirements because the element 9 is repeated.
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions

Example 2:

Input: nums = [4,4,4], k = 3
Output: 0
Explanation: The subarrays of nums with length 3 are:
- [4,4,4] which does not meet the requirements because the element 4 is repeated.
We return 0 because no subarrays meet the conditions.

  Constraints:

  • 1 <= k <= nums.length <= 105

  • 1 <= nums[i] <= 105

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

C++

  • class Solution {
    public:
        long long maximumSubarraySum(vector<int>& nums, int k) {
            int n = nums.size();
            unordered_map<int, int> cnt;
            long long s = 0, ans = 0;
            for (int i = 0; i < k; ++i) {
                cnt[nums[i]]++;
                s += nums[i];
            }
            for (int i = k; i < n; ++i) {
                if (cnt.size() == k) ans = max(ans, s);
                cnt[nums[i]]++;
                cnt[nums[i - k]]--;
                if (cnt[nums[i - k]] == 0) cnt.erase(nums[i - k]);
                s += nums[i];
                s -= nums[i - k];
            }
            if (cnt.size() == k) ans = max(ans, s);
            return ans;
        }
    };

JAVA 

  • class Solution {
        public long maximumSubarraySum(int[] nums, int k) {
            int n = nums.length;
            Map<Integer, Integer> cnt = new HashMap<>(k);
            long s = 0;
            for (int i = 0; i < k; ++i) {
                cnt.put(nums[i], cnt.getOrDefault(nums[i], 0) + 1);
                s += nums[i];
            }
            long ans = 0;
            for (int i = k; i < n; ++i) {
                if (cnt.size() == k) {
                    ans = Math.max(ans, s);
                }
                cnt.put(nums[i], cnt.getOrDefault(nums[i], 0) + 1);
                cnt.put(nums[i - k], cnt.getOrDefault(nums[i - k], 0) - 1);
                if (cnt.get(nums[i - k]) == 0) {
                    cnt.remove(nums[i - k]);
                }
                s += nums[i];
                s -= nums[i - k];
            }
            if (cnt.size() == k) {
                ans = Math.max(ans, s);
            }
            return ans;
        }
    }

PYTHON

  • class Solution:
        def maximumSubarraySum(self, nums: list[int], k: int) -> int:
            from collections import defaultdict
    
            n = len(nums)
            cnt = defaultdict(int)
            s = 0
            ans = 0
    
            for i in range(k):
                cnt[nums[i]] += 1
                s += nums[i]
    
            for i in range(k, n):
                if len(cnt) == k:
                    ans = max(ans, s)
                cnt[nums[i]] += 1
                cnt[nums[i - k]] -= 1
                if cnt[nums[i - k]] == 0:
                    del cnt[nums[i - k]]
                s += nums[i]
                s -= nums[i - k]
    
            if len(cnt) == k:
                ans = max(ans, s)
    
            return ans

Comments