c++ code with through explanation
When you move the right cursor, keep tracking whether we have reach a count of K distinct integers, if yes, we process left cursor, here is how we process left 
When you move the right cursor, keep tracking whether we have reach a count of K distinct integers, if yes, we process left cursor, here is how we process left cursor:\n\ncheck whether the element pointed by left cursor is duplicated in window, if yes, we remove it, and use a variable (e.g. prefix) to record that we have removed an element from the window). keep this process until we reduce the window size from to exactly K. now we can calculate the number of valid good array as res += prefix;\n\nafter process left cursor and all the stuff, the outer loop will continue and right cursor will move forward, and then the window size will exceed K, we can simply drop the left most element of the window and reset prefix to 0. and continue on.\n\n\n\n\n```cpp\nclass Solution {\npublic:\n    int subarraysWithKDistinct(vector<int>& A, int K) {\n        int res = 0;\n      \tvector<int> m(A.size() + 1);\n        int end = 0, begin = 0, prefix = 0, cnt = 0;\n      \twhile(end < A.size()) {\n        \tif (m[A[end++]]++ == 0) ++cnt;\n        \tif (cnt > K) {\n                --m[A[begin++]];\n                --cnt;\n                prefix = 0;\n            }\n        \twhile (m[A[begin]] > 1) {\n                ++prefix; \n                --m[A[begin++]];\n            }\n        \tif (cnt == K) \n                res += prefix + 1;\n      \t}\n      \treturn res;\n    }\n};\n```\n\n![image](https://assets.leetcode.com/users/juniway/image_1589794387.png)\n
[]