Subarrays with exactly k distinct
Your task
Given an array nums of positive integers and an integer k, return the number of subarrays (contiguous) that contain exactly k distinct integers. Constraints: 1 <= len(nums) <= 2*10^4, 1 <= nums[i] <= len(nums), 1 <= k <= len(nums).
Examples
subarrays_k_distinct(nums: list[int], k: int) -> intExample 1
Input
subarrays_k_distinct([1, 2, 1, 2, 3], 2)Output
7