[cpp] Sliding window O(N) solution
Use a hashmap to store the frequency of each num, use a variable cnt to store the the number of different integers in the window. \n1. Slide the right border j 
Use a hashmap to store the frequency of each num, use a variable `cnt` to store the the number of different integers in the window. \n1. Slide the right border `j` : If we meet a num with `hash[j] == 0, cnt += 1`. Find a sliding window with `k` different numbers . \n2. Slide left border `i` if `hash[A[i]] > 1`, which means that there are more than one `A[i]` in the window. If we meet `hash[A[i]] == 1` we cannot slide `i`more. Now we can build subarrays which start from `the previous i(istart)` to `i` and ends with `j `, and each of them has k different  integers in it . That adds `i - istart + 1` to our final answer. \n3. Move on with the right border `j`, there are two situations:\n* If our `cnt` changes, we must move our left border `i` until `cnt == k` and reset `istart`.\n* If our `cnt` still equals to `k`(which means `A[j]` eauals to some existing value in the window), the subarrays which start from `istart` to `i` and ends with new `j` will still work(no more different number is added). We just need to move the left border `i` since the right border `j` may have same number with the left border and we only need `one` number to maintain the window. If we cannot slide more(`hash[A[i]] == 1`) , update our answer.\n\nSince `1 <= A[i] <= A.length`, we can use a vector instead of hashmap.\n**Complexity:**\n* Time: O(N)\n* Space: O(N)\n\n```\nint subarraysWithKDistinct(vector<int>& A, int k) {\n        int n = A.size(), ans = 0;\n        //unordered_map<int, int> hash;\n\t\tvector<int> hash(n + 1, 0);\n        for(int i = 0, j = 0, cnt = 0, istart = 0; j < n; ++j) {\n            if(hash[A[j]]++ == 0) cnt++;\n            if(cnt < k) continue;\n            while(cnt > k) {\n                cnt -= (0 == --hash[A[i++]]);\n                istart = i;\n            } \n            while(hash[A[i]] > 1) hash[A[i++]]--;\n            ans += i - istart + 1;\n        }\n        return ans;\n    }\n```
[]