[Java] O(N^2) short and easy understanding
A better implementation should be using sliding window whose time complexity is O(N). However, the bruteforce way is very intuitive that could pass OJ so just s
A better implementation should be using sliding window whose time complexity is O(N). However, the bruteforce way is very intuitive that could pass OJ so just share here.\n\nFor each subarray, check if there are K distinct intergers.\n\n```\n    public int subarraysWithKDistinct(int[] A, int k) {\n        int res = 0, n = A.length;\n        int[] cnt = new int[n + 1]; // Given 1 <= A.length <= 20000 and 1 <= A[i] <= A.length\n        \n        for(int i = 0; i < n; i++) {\n            int distCount = 0;\n            Arrays.fill(cnt, 0);\n            for(int j = i; j < n; j++) {\n                if(cnt[A[j]] == 0) distCount++;\n                cnt[A[j]]++;\n                if(distCount == k) res++;\n            }\n        }\n        return res;\n    }\n```
[]