Python, Clean, Sliding Window + 2 Pointers, O(n), 99.06%
\nclass Solution:\n    def subarraysWithKDistinct(self, A: \'List[int]\', K: \'int\') -> \'int\':\n        freq = {}\n        start = 0\n        start_k = 0\n  
```\nclass Solution:\n    def subarraysWithKDistinct(self, A: \'List[int]\', K: \'int\') -> \'int\':\n        freq = {}\n        start = 0\n        start_k = 0\n        res = 0\n        for i, x in enumerate(A):\n            freq[x] = freq.get(x, 0) + 1\n            if len(freq) == K + 1:\n                # remove the distinct at start_k, move start_k, start\n                del freq[A[start_k]]\n                start_k += 1\n                start = start_k\n            if len(freq) == K:\n                # update start_k and res (Notice: K >= 1)\n                while freq[A[start_k]] > 1:\n                    freq[A[start_k]] -= 1\n                    start_k += 1\n                res += start_k - start + 1\n        return res\n```
[]