python3 Solution
\n\nclass Solution:\n    def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:\n        cnt=[0]*(len(nums)+1)\n        ans=0\n        l=0\n        m
\n```\nclass Solution:\n    def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:\n        cnt=[0]*(len(nums)+1)\n        ans=0\n        l=0\n        m=0\n        for num in nums:\n            cnt[num]+=1\n            if cnt[num]==1:\n                k-=1\n                if k<0:\n                    cnt[nums[m]]=0\n                    m+=1\n                    l=m\n            if k<=0:\n                while cnt[nums[m]]>1:\n                    cnt[nums[m]]-=1\n                    m+=1\n                ans+=m-l+1\n        return ans                        \n```
['Python', 'Python3']