Python3 O(n) OrderedDict + Sliding Window - Subarrays with K Different Integers
\nfrom collections import OrderedDict\n\nclass Solution:\n    def subarraysWithKDistinct(self, A: List[int], K: int) -> int:\n        ans = l = 0\n        # Las
```\nfrom collections import OrderedDict\n\nclass Solution:\n    def subarraysWithKDistinct(self, A: List[int], K: int) -> int:\n        ans = l = 0\n        # Last seen index of an integer\n        od = OrderedDict()\n        for i, n in enumerate(A):\n            od[n] = i\n            od.move_to_end(n)\n            while len(od) > K:\n                l = od.popitem(last=False)[1] + 1\n            if len(od) == K:\n\t\t\t    # The smallest index in od - left bound + 1\n                ans += next(iter(od.items()))[1] - l + 1\n        return ans\n```
['Sliding Window', 'Python', 'Python3']