Shortest Window by Distinct-Value Sum
Problem statement
For a contiguous window in nums, its distinct-value sum adds each different value exactly once. The reported example gives [3,3,2] a sum of 5, because the repeated 3 is counted once.
Return the minimum number of positions in a nonempty contiguous window whose distinct-value sum is at least target. Count every position in its length, including repeated values.
For this exercise, assume all array values and the target are positive integers. Return 0 if no window qualifies, including when the array is empty. Keep the input unchanged.
Function
minimumDistinctSumWindow(nums: int[], target: int) → intExamples
Example 1
nums = [3,3,2]target = 5return = 2The last two positions contain 3 and 2, whose distinct-value sum is 5. No singleton reaches 5; the full three-position window also has sum 5 but is longer.
Example 2
nums = [1,1,1]target = 2return = 0Every nonempty window contains only the distinct value 1, so its distinct-value sum is 1 and cannot reach 2.
Example 3
nums = []target = 1return = 0There is no nonempty window.
Constraints
- For this exercise, assume
0 <= nums.length <= 1000. - For this exercise, assume
1 <= nums[i] <= 1000000. - For this exercise, assume
1 <= target <= 1000000000.