Maximum Sum of Distinct Subarrays With Length K
MediumSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.Given an array nums and an integer k, consider every subarray of length k whose elements are all distinct. Return the largest sum among them, or 0 if no such subarray exists.
nums = [1, 5, 4, 2, 9, 9, 9], k = 3 → 15 ([5, 4, 2] = 11, [4, 2, 9] = 15)
nums = [4, 4, 4], k = 3 → 0Constraints
1 <= k <= nums.length <= 10^51 <= nums[i] <= 10^5