You are given a 0-indexed integer array candies, where candies[i]
represents the flavor of the ith candy. Your mom wants you to share these candies with your little sister by giving her kconsecutive candies, but you want to keep as many flavors of candies as possible.
Return _themaximum number of unique flavors of candy you can keep after sharing _with your sister.
Input: candies =[1,_2,2,3_,4,3], k =3Output: 3Explanation:
Give the candies in the range [1,3](inclusive)with flavors [2,2,3].You can eat candies with flavors [1,4,3].There are 3 unique flavors, so return3.
Example 2:
1
2
3
4
5
6
7
Input: candies =[2,2,2,_2,3_,3], k =2Output: 2Explanation:
Give the candies in the range [3,4](inclusive)with flavors [2,3].You can eat candies with flavors [2,2,2,3].There are 2 unique flavors, so return2.Note that you can also share the candies with flavors [2,2] and eat the candies with flavors [2,2,3,3].
Example 3:
1
2
3
4
5
6
Input: candies =[2,4,5], k =0Output: 3Explanation:
You do not have to give any candies.You can eat the candies with flavors [2,4,5].There are 3 unique flavors, so return3.
To maximize the number of unique flavors you keep, you want to minimize the number of unique flavors in the k-length window you give away. For each possible window, count the unique flavors outside the window. Use a sliding window and a hash map to efficiently track the counts.
#include<unordered_map>classSolution {
public:int shareCandies(vector<int>& candies, int k) {
unordered_map<int, int> freq;
for (int c : candies) freq[c]++;
int n = candies.size(), ans =0;
unordered_map<int, int> window;
for (int i =0; i < k; ++i) window[candies[i]]++;
for (int i =0; i <= n - k; ++i) {
for (auto& [f, cnt] : window) freq[f] -= cnt;
int cur =0;
for (auto& [f, cnt] : freq) if (cnt >0) cur++;
ans = max(ans, cur);
for (auto& [f, cnt] : window) freq[f] += cnt;
if (i + k < n) {
window[candies[i]]--;
if (window[candies[i]] ==0) window.erase(candies[i]);
window[candies[i + k]]++;
}
}
return ans;
}
};
import java.util.*;
classSolution {
publicintshareCandies(int[] candies, int k) {
Map<Integer, Integer> freq =new HashMap<>();
for (int c : candies) freq.put(c, freq.getOrDefault(c, 0) + 1);
int n = candies.length, ans = 0;
Map<Integer, Integer> window =new HashMap<>();
for (int i = 0; i < k; i++) window.put(candies[i], window.getOrDefault(candies[i], 0) + 1);
for (int i = 0; i <= n - k; i++) {
for (Map.Entry<Integer, Integer> e : window.entrySet()) freq.put(e.getKey(), freq.get(e.getKey()) - e.getValue());
int cur = 0;
for (int v : freq.values()) if (v > 0) cur++;
ans = Math.max(ans, cur);
for (Map.Entry<Integer, Integer> e : window.entrySet()) freq.put(e.getKey(), freq.get(e.getKey()) + e.getValue());
if (i + k < n) {
window.put(candies[i], window.get(candies[i]) - 1);
if (window.get(candies[i]) == 0) window.remove(candies[i]);
window.put(candies[i + k], window.getOrDefault(candies[i + k], 0) + 1);
}
}
return ans;
}
}
classSolution {
funshareCandies(candies: IntArray, k: Int): Int {
val freq = mutableMapOf<Int, Int>()
for (c in candies) freq[c] = freq.getOrDefault(c, 0) + 1val n = candies.size
var ans = 0val window = mutableMapOf<Int, Int>()
for (i in0 until k) window[candies[i]] = window.getOrDefault(candies[i], 0) + 1for (i in0..n - k) {
for ((f, cnt) in window) freq[f] = freq[f]!! - cnt
val cur = freq.values.count { it > 0 }
ans = maxOf(ans, cur)
for ((f, cnt) in window) freq[f] = freq[f]!! + cnt
if (i + k < n) {
window[candies[i]] = window[candies[i]]!! - 1if (window[candies[i]] ==0) window.remove(candies[i])
window[candies[i + k]] = window.getOrDefault(candies[i + k], 0) + 1 }
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defshareCandies(self, candies: list[int], k: int) -> int:
from collections import Counter
freq = Counter(candies)
n = len(candies)
ans =0 window = Counter(candies[:k])
for i in range(n - k +1):
for f in window: freq[f] -= window[f]
cur = sum(1for v in freq.values() if v >0)
ans = max(ans, cur)
for f in window: freq[f] += window[f]
if i + k < n:
window[candies[i]] -=1if window[candies[i]] ==0: del window[candies[i]]
window[candies[i + k]] +=1return ans
use std::collections::HashMap;
impl Solution {
pubfnshare_candies(candies: Vec<i32>, k: i32) -> i32 {
letmut freq = HashMap::new();
for&c in&candies { *freq.entry(c).or_insert(0) +=1; }
let n = candies.len();
letmut ans =0;
letmut window = HashMap::new();
for i in0..k asusize { *window.entry(candies[i]).or_insert(0) +=1; }
for i in0..=n - k asusize {
for (&f, &cnt) in&window { *freq.get_mut(&f).unwrap() -= cnt; }
let cur = freq.values().filter(|&&v| v >0).count();
ans = ans.max(cur);
for (&f, &cnt) in&window { *freq.get_mut(&f).unwrap() += cnt; }
if i + k asusize< n {
*window.get_mut(&candies[i]).unwrap() -=1;
if window[&candies[i]] ==0 { window.remove(&candies[i]); }
*window.entry(candies[i + k asusize]).or_insert(0) +=1;
}
}
ans asi32 }
}