> For the complete documentation index, see [llms.txt](https://garylai.gitbook.io/algorithm-and-data-structure/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://garylai.gitbook.io/algorithm-and-data-structure/problems/sliding-window/subarrays-with-k-different-integers-1.md).

# 992. Subarrays with K Different Integers

[992. Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/)

先見算出「最多」 k 個不同的數字的組合，接著再算出「最多」 k -1 個不同數字的組合，兩個數字的差就是「剛好」有 k 個數字的組合。

算出最多有 k 個不同的數字的組合，可以用滑動窗口的方式來計算。

算出最多有 k 個不同的數字的組合可以參考 [340. Longest Substring with At Most K Distinct Character](/algorithm-and-data-structure/problems/sliding-window/longest-substring-with-at-most-k-distinct-character.md) 。

```python
class Solution:
    def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:
        def atMostK(k):
            fast = 0
            slow = 0
            memo = defaultdict(int)
            res = 0
            while fast < len(nums):
                char = nums[fast]
                fast += 1
                
                memo[char] += 1
                
                while len(memo) > k:
                    d = nums[slow]
                    slow += 1
                    memo[d] -= 1
                    if memo[d] == 0:
                        del memo[d]
                res += fast - slow
            return res
        return atMostK(k) - atMostK(k - 1)
```
