Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int colorfulCandies(int N, int K, int[] color) {
- Map<Integer, Integer> countMap = new HashMap<>();
- int maxDistinctColors = 0;
- for (int i = 0, j = 0; i < N; i++) {
- countMap.put(color[i], countMap.getOrDefault(color[i], 0) + 1);
- if (i - j + 1 > K) {
- int count = countMap.get(color[j]);
- if (count > 1) {
- countMap.put(color[j], count - 1);
- } else {
- countMap.remove(color[j]);
- }
- j++;
- }
- maxDistinctColors = Math.max(maxDistinctColors, countMap.size());
- }
- return maxDistinctColors;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement