Count runs with exactly k distinct values
Given a list of integers nums and an integer k >= 1, count the contiguous, non-empty subarrays that contain exactly k different values. Subarrays at different positions count separately, even when they hold the same values: for [1, 2, 1] and k = 2 the answer is 3 ([1, 2], [2, 1] and [1, 2, 1]).
nums may be empty. Aim for O(n) time.
Besides the tests, one large input (4,000 values) with a ~200 ms limit (scaled to your device's speed): brute force won't pass.
Examples
count_exact_distinct([3,3,5,3], 2)→5count_exact_distinct([4,4,4], 1)→6count_exact_distinct([1,2,3,1,2], 3)→6