<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>

class Solution {
public:
    int subarraysWithKDistinct(vector<int>& nums, int k) {
        int n = nums.size();
        vector<int> nearest_good(n);
        vector<int> farthest_good(n);
        int num_unq = 0;
        map<int,int> hash1;
        map<int,int> hash2;
        int l,r;

        l = 0;
        r = -1;

        while(l < n)
        {
            if(num_unq == k)
            {
                nearest_good[l] = r;
                hash1[nums[l]]--;
                if(hash1[nums[l]] == 0)
                    num_unq--;
                l++;
            }
            else
            {
                if(r == n-1)
                {
                    nearest_good[l] = -1;
                    hash1[nums[l]]--;
                    if(hash1[nums[l]] == 0)
                        num_unq--;
                    l++;
                    continue;
                }

                if(hash1[nums[r+1]] > 0)
                {
                    hash1[nums[r+1]]++;
                    r++;
                }
                else
                {
                    hash1[nums[r+1]]++;
                    r++;
                    num_unq++;
                }
            }
        }

        l = 0;
        r = -1;
        num_unq = 0;

        while(l < n)
        {
            if(num_unq == k)
            {
                if(r == n-1)
                {
                    farthest_good[l] = r;
                    hash1[nums[l]]--;
                    if(hash1[nums[l]] == 0)
                        num_unq--;
                    l++;
                    continue;
                }

                if(hash2[nums[r+1]] > 0)
                {
                    hash2[nums[r+1]]++;
                    r++;
                }
                else
                {
                    farthest_good[l] = r;
                    hash2[nums[l]]--;
                    if(hash2[nums[l]] == 0)
                        num_unq--;
                    l++;
                }
            }
            else
            {
                if(r == n-1)
                {
                    farthest_good[l] = -1;
                    hash1[nums[l]]--;
                    if(hash1[nums[l]] == 0)
                        num_unq--;
                    l++;
                    continue;
                }

                if(hash2[nums[r+1]] > 0)
                {
                    hash2[nums[r+1]]++;
                    r++;
                }
                else
                {
                    hash2[nums[r+1]]++;
                    num_unq++;
                    r++;
                }
            }
        }

        int ans = 0;

        for(int i=0;i<n;i++)
        {
            if(nearest_good[i] == -1)
                break;
            
            ans += (farthest_good[i] - nearest_good[i] + 1);
        }

        return ans;

    }
};
57085