Subarrays With K Distinct
Given an integer array nums and a positive integer k, return the number of non-empty contiguous subarrays that contain exactly k distinct values.
Given an integer array `nums` and a positive integer `k`, return the number of non-empty contiguous subarrays that contain exactly `k` distinct values.
- `1 <= nums.length <= 2 * 10^4` - `-1000 <= nums[i] <= 1000` - `1 <= k <= nums.length` - The interactive visualizer accepts at most 12 values; longer valid arrays are execution-only.
Example 1: input nums = [1,2,1,2,3], k = 2; output 7. Seven contiguous ranges contain exactly two distinct values.
Example 2: input nums = [1,2,1,3,4], k = 3; output 3. The qualifying ranges are [1,2,1,3], [2,1,3], and [1,3,4].
Category: Sliding Window. Topic: sliding-window. Difficulty: Hard. Patterns: hashing, sliding-window.