<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>good subarrays</strong> of </em><code>nums</code>.</p>

<p>A <strong>good array</strong> is an array where the number of different integers in that array is exactly <code>k</code>.</p>

<ul>
	<li>For example, <code>[1,2,3,1,2]</code> has <code>3</code> different integers: <code>1</code>, <code>2</code>, and <code>3</code>.</li>
</ul>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,1,2,3], k = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,1,3,4], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i], k &lt;= nums.length</code></li>
</ul>

from collections import defaultdict, deque

class Solution:
    def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:
        n_to_inds = defaultdict(deque)

        res = 0
        left, last = 0, None

        def inc_left():
            """ We've just hit more than k. """
            nonlocal left, last
            inds = n_to_inds[nums[left]]
            inds.popleft()

            if len(inds) == 0:
                del n_to_inds[nums[left]]
                last = None
            else:
                nxt = inds[0]
                last = max(last, nxt)

            left += 1


        for right_i, n in enumerate(nums + [-1]):
            #print(left, last, right_i, n, dict(n_to_inds), res)
            n_to_inds[n].append(right_i)
            curr_amt = len(n_to_inds)

            if curr_amt < k:
                continue

            while len(n_to_inds) > k:
                #print("Inc left: ", left, last, right_i, n, dict(n_to_inds), res)
                res += right_i - last
                inc_left()

            if last is None:
                last = right_i

        return res
            
        
20300