[Java/C++/Python] Sliding Window
I republished this post.\nThe original one is deleded by Leetcode without any notification or information.\nThe only reason that, I included my youtube channel 
**I republished this post.\nThe original one is deleded by Leetcode without any notification or information.\nThe only reason that, I included my youtube channel link.\nExcusem me, What the HACK!?**\n<br>\n\n## **Intuition**:\nFirst you may have feeling of using sliding window.\nThen this idea get stuck in the middle.\n\nThis problem will be a very typical sliding window,\nif it asks the number of subarrays with **at most** K distinct elements.\n\nJust need one more step to reach the folloing equation:\n`exactly(K) = atMost(K) - atMost(K-1)`\n<br>\n\n## **Explanation**\n1. Write/copy a helper function of sliding window,\nto get the number of subarrays with at most K distinct elements.\n2. Done.\n<br>\n\n## **Complexity**:\nTime `O(N)` for two passes.\nSpace `O(K)` at most K elements in the counter\n\nOf course, you can merge 2 for loops into one, if you like.\n<br>\n\n**Java:**\n```java\n    public int subarraysWithKDistinct(int[] A, int K) {\n        return atMostK(A, K) - atMostK(A, K - 1);\n    }\n    int atMostK(int[] A, int K) {\n        int i = 0, res = 0;\n        Map<Integer, Integer> count = new HashMap<>();\n        for (int j = 0; j < A.length; ++j) {\n            if (count.getOrDefault(A[j], 0) == 0) K--;\n            count.put(A[j], count.getOrDefault(A[j], 0) + 1);\n            while (K < 0) {\n                count.put(A[i], count.get(A[i]) - 1);\n                if (count.get(A[i]) == 0) K++;\n                i++;\n            }\n            res += j - i + 1;\n        }\n        return res;\n    }\n```\n\n**C++:**\n```cpp\n    int subarraysWithKDistinct(vector<int>& A, int K) {\n        return atMostK(A, K) - atMostK(A, K - 1);\n    }\n    int atMostK(vector<int>& A, int K) {\n        int i = 0, res = 0;\n        unordered_map<int, int> count;\n        for (int j = 0; j < A.size(); ++j) {\n            if (!count[A[j]]++) K--;\n            while (K < 0) {\n                if (!--count[A[i]]) K++;\n                i++;\n            }\n            res += j - i + 1;\n        }\n        return res;\n    }\n```\n\n**Python:**\n```python\n    def subarraysWithKDistinct(self, A, K):\n        return self.atMostK(A, K) - self.atMostK(A, K - 1)\n\n    def atMostK(self, A, K):\n        count = collections.Counter()\n        res = i = 0\n        for j in range(len(A)):\n            if count[A[j]] == 0: K -= 1\n            count[A[j]] += 1\n            while K < 0:\n                count[A[i]] -= 1\n                if count[A[i]] == 0: K += 1\n                i += 1\n            res += j - i + 1\n        return res\n```\n<br>\n\n# More Similar Sliding Window Problems\nHere are some similar sliding window problems.\nAlso find more explanations.\nGood luck and have fun.\n\n\n- 1358. [Number of Substrings Containing All Three Characters](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/discuss/516977/JavaC++Python-Easy-and-Concise)\n- 1248. [Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/discuss/419378/JavaC%2B%2BPython-Sliding-Window-atMost(K)-atMost(K-1))\n- 1234. [Replace the Substring for Balanced String](https://leetcode.com/problems/replace-the-substring-for-balanced-string/discuss/408978/javacpython-sliding-window/367697)\n- 1004. [Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/discuss/247564/javacpython-sliding-window/379427?page=3)\n-  930. [Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/discuss/186683/)\n-  992. [Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/234482/JavaC%2B%2BPython-Sliding-Window-atMost(K)-atMost(K-1))\n-  904. [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/discuss/170740/Sliding-Window-for-K-Elements)\n-  862. [Shortest Subarray with Sum at Least K](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/discuss/143726/C%2B%2BJavaPython-O(N)-Using-Deque)\n-  209. [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/discuss/433123/JavaC++Python-Sliding-Window)\n<br>
[]