Subarrays with exactly k distinct

Write codeHard32 minMeta and Amazon style

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) -> int
Example 1
Inputsubarrays_k_distinct([1, 2, 1, 2, 3], 2)
Output7