diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README.md b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README.md index ea77fad73ff72..374f810e52de2 100644 --- a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README.md +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README.md @@ -83,8 +83,8 @@ class Solution: cnt = Counter(candies[k:]) ans = len(cnt) for i in range(k, len(candies)): - cnt[candies[i]] -= 1 cnt[candies[i - k]] += 1 + cnt[candies[i]] -= 1 if cnt[candies[i]] == 0: cnt.pop(candies[i]) ans = max(ans, len(cnt)) @@ -104,11 +104,11 @@ class Solution { cnt.merge(candies[i], 1, Integer::sum); } int ans = cnt.size(); - for (int i = k; i < candies.length; ++i) { + for (int i = k; i < n; ++i) { + cnt.merge(candies[i - k], 1, Integer::sum); if (cnt.merge(candies[i], -1, Integer::sum) == 0) { cnt.remove(candies[i]); } - cnt.merge(candies[i - k], 1, Integer::sum); ans = Math.max(ans, cnt.size()); } return ans; @@ -128,11 +128,11 @@ public: ++cnt[candies[i]]; } int ans = cnt.size(); - for (int i = k; i < candies.size(); ++i) { + for (int i = k; i < n; ++i) { + ++cnt[candies[i - k]]; if (--cnt[candies[i]] == 0) { cnt.erase(candies[i]); } - ++cnt[candies[i - k]]; ans = max(ans, (int) cnt.size()); } return ans; @@ -150,21 +150,69 @@ func shareCandies(candies []int, k int) (ans int) { } ans = len(cnt) for i := k; i < len(candies); i++ { + cnt[candies[i-k]]++ cnt[candies[i]]-- if cnt[candies[i]] == 0 { delete(cnt, candies[i]) } - cnt[candies[i-k]]++ ans = max(ans, len(cnt)) } return } ``` +### **Rust** + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn share_candies(candies: Vec, k: i32) -> i32 { + let mut cnt = HashMap::new(); + let n = candies.len(); + + for i in k as usize..n { + *cnt.entry(candies[i]).or_insert(0) += 1; + } + + let mut ans = cnt.len() as i32; + + for i in k as usize..n { + *cnt.entry(candies[i - k as usize]).or_insert(0) += 1; + if let Some(x) = cnt.get_mut(&candies[i]) { + *x -= 1; + if *x == 0 { + cnt.remove(&candies[i]); + } + } + + ans = ans.max(cnt.len() as i32); + } + + ans + } +} +``` + ### **TypeScript** ```ts - +function shareCandies(candies: number[], k: number): number { + const cnt: Map = new Map(); + for (const x of candies.slice(k)) { + cnt.set(x, (cnt.get(x) || 0) + 1); + } + let ans = cnt.size; + for (let i = k; i < candies.length; ++i) { + cnt.set(candies[i - k], (cnt.get(candies[i - k]) || 0) + 1); + cnt.set(candies[i], (cnt.get(candies[i]) || 0) - 1); + if (cnt.get(candies[i]) === 0) { + cnt.delete(candies[i]); + } + ans = Math.max(ans, cnt.size); + } + return ans; +} ``` ### **...** diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README_EN.md b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README_EN.md index 39e073f92468d..e73ab838f78bb 100644 --- a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README_EN.md +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README_EN.md @@ -54,6 +54,18 @@ There are 3 unique flavors, so return 3. ## Solutions +**Solution 1: Sliding Window + Hash Table** + +We can maintain a sliding window of size $k$, where the candies outside the window are for ourselves, and the $k$ candies inside the window are shared with our sister and mother. We can use a hash table $cnt$ to record the flavors of the candies outside the window and their corresponding quantities. + +Initially, the hash table $cnt$ stores the flavors of the candies from $candies[k]$ to $candies[n-1]$ and their corresponding quantities. At this time, the number of candy flavors is the size of the hash table $cnt$, that is, $ans = cnt.size()$. + +Next, we traverse the candies in the range $[k,..n-1]$, add the current candy $candies[i]$ to the window, and move the candy $candies[i-k]$ on the left side of the window out of the window. Then we update the hash table $cnt$, and update the number of candy flavors $ans$ to $max(ans, cnt.size())$. + +After traversing all the candies, we can get the maximum number of unique flavors of candies that can be retained. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of candies. + ### **Python3** @@ -64,8 +76,8 @@ class Solution: cnt = Counter(candies[k:]) ans = len(cnt) for i in range(k, len(candies)): - cnt[candies[i]] -= 1 cnt[candies[i - k]] += 1 + cnt[candies[i]] -= 1 if cnt[candies[i]] == 0: cnt.pop(candies[i]) ans = max(ans, len(cnt)) @@ -83,11 +95,11 @@ class Solution { cnt.merge(candies[i], 1, Integer::sum); } int ans = cnt.size(); - for (int i = k; i < candies.length; ++i) { + for (int i = k; i < n; ++i) { + cnt.merge(candies[i - k], 1, Integer::sum); if (cnt.merge(candies[i], -1, Integer::sum) == 0) { cnt.remove(candies[i]); } - cnt.merge(candies[i - k], 1, Integer::sum); ans = Math.max(ans, cnt.size()); } return ans; @@ -107,11 +119,11 @@ public: ++cnt[candies[i]]; } int ans = cnt.size(); - for (int i = k; i < candies.size(); ++i) { + for (int i = k; i < n; ++i) { + ++cnt[candies[i - k]]; if (--cnt[candies[i]] == 0) { cnt.erase(candies[i]); } - ++cnt[candies[i - k]]; ans = max(ans, (int) cnt.size()); } return ans; @@ -129,21 +141,69 @@ func shareCandies(candies []int, k int) (ans int) { } ans = len(cnt) for i := k; i < len(candies); i++ { + cnt[candies[i-k]]++ cnt[candies[i]]-- if cnt[candies[i]] == 0 { delete(cnt, candies[i]) } - cnt[candies[i-k]]++ ans = max(ans, len(cnt)) } return } ``` +### **Rust** + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn share_candies(candies: Vec, k: i32) -> i32 { + let mut cnt = HashMap::new(); + let n = candies.len(); + + for i in k as usize..n { + *cnt.entry(candies[i]).or_insert(0) += 1; + } + + let mut ans = cnt.len() as i32; + + for i in k as usize..n { + *cnt.entry(candies[i - k as usize]).or_insert(0) += 1; + if let Some(x) = cnt.get_mut(&candies[i]) { + *x -= 1; + if *x == 0 { + cnt.remove(&candies[i]); + } + } + + ans = ans.max(cnt.len() as i32); + } + + ans + } +} +``` + ### **TypeScript** ```ts - +function shareCandies(candies: number[], k: number): number { + const cnt: Map = new Map(); + for (const x of candies.slice(k)) { + cnt.set(x, (cnt.get(x) || 0) + 1); + } + let ans = cnt.size; + for (let i = k; i < candies.length; ++i) { + cnt.set(candies[i - k], (cnt.get(candies[i - k]) || 0) + 1); + cnt.set(candies[i], (cnt.get(candies[i]) || 0) - 1); + if (cnt.get(candies[i]) === 0) { + cnt.delete(candies[i]); + } + ans = Math.max(ans, cnt.size); + } + return ans; +} ``` ### **...** diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.cpp b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.cpp index 48783ba1bc6c1..e1a1d1540752e 100644 --- a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.cpp +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.cpp @@ -1,19 +1,19 @@ -class Solution { -public: - int shareCandies(vector& candies, int k) { - unordered_map cnt; - int n = candies.size(); - for (int i = k; i < n; ++i) { - ++cnt[candies[i]]; - } - int ans = cnt.size(); - for (int i = k; i < candies.size(); ++i) { - if (--cnt[candies[i]] == 0) { - cnt.erase(candies[i]); - } - ++cnt[candies[i - k]]; - ans = max(ans, (int) cnt.size()); - } - return ans; - } +class Solution { +public: + int shareCandies(vector& candies, int k) { + unordered_map cnt; + int n = candies.size(); + for (int i = k; i < n; ++i) { + ++cnt[candies[i]]; + } + int ans = cnt.size(); + for (int i = k; i < n; ++i) { + ++cnt[candies[i - k]]; + if (--cnt[candies[i]] == 0) { + cnt.erase(candies[i]); + } + ans = max(ans, (int) cnt.size()); + } + return ans; + } }; \ No newline at end of file diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.go b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.go index 5ded76486e1ec..f92e7c0878d86 100644 --- a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.go +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.go @@ -5,11 +5,11 @@ func shareCandies(candies []int, k int) (ans int) { } ans = len(cnt) for i := k; i < len(candies); i++ { + cnt[candies[i-k]]++ cnt[candies[i]]-- if cnt[candies[i]] == 0 { delete(cnt, candies[i]) } - cnt[candies[i-k]]++ ans = max(ans, len(cnt)) } return diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.java b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.java index 968f96e43d081..6bdb8089df6a2 100644 --- a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.java +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.java @@ -1,18 +1,18 @@ -class Solution { - public int shareCandies(int[] candies, int k) { - Map cnt = new HashMap<>(); - int n = candies.length; - for (int i = k; i < n; ++i) { - cnt.merge(candies[i], 1, Integer::sum); - } - int ans = cnt.size(); - for (int i = k; i < candies.length; ++i) { - if (cnt.merge(candies[i], -1, Integer::sum) == 0) { - cnt.remove(candies[i]); - } - cnt.merge(candies[i - k], 1, Integer::sum); - ans = Math.max(ans, cnt.size()); - } - return ans; - } +class Solution { + public int shareCandies(int[] candies, int k) { + Map cnt = new HashMap<>(); + int n = candies.length; + for (int i = k; i < n; ++i) { + cnt.merge(candies[i], 1, Integer::sum); + } + int ans = cnt.size(); + for (int i = k; i < n; ++i) { + cnt.merge(candies[i - k], 1, Integer::sum); + if (cnt.merge(candies[i], -1, Integer::sum) == 0) { + cnt.remove(candies[i]); + } + ans = Math.max(ans, cnt.size()); + } + return ans; + } } \ No newline at end of file diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.py b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.py index cb902e0fdbd49..b7a262f81700e 100644 --- a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.py +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.py @@ -1,11 +1,11 @@ -class Solution: - def shareCandies(self, candies: List[int], k: int) -> int: - cnt = Counter(candies[k:]) - ans = len(cnt) - for i in range(k, len(candies)): - cnt[candies[i]] -= 1 - cnt[candies[i - k]] += 1 - if cnt[candies[i]] == 0: - cnt.pop(candies[i]) - ans = max(ans, len(cnt)) - return ans +class Solution: + def shareCandies(self, candies: List[int], k: int) -> int: + cnt = Counter(candies[k:]) + ans = len(cnt) + for i in range(k, len(candies)): + cnt[candies[i - k]] += 1 + cnt[candies[i]] -= 1 + if cnt[candies[i]] == 0: + cnt.pop(candies[i]) + ans = max(ans, len(cnt)) + return ans diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.rs b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.rs new file mode 100644 index 0000000000000..cd52d94851c26 --- /dev/null +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.rs @@ -0,0 +1,28 @@ +use std::collections::HashMap; + +impl Solution { + pub fn share_candies(candies: Vec, k: i32) -> i32 { + let mut cnt = HashMap::new(); + let n = candies.len(); + + for i in k as usize..n { + *cnt.entry(candies[i]).or_insert(0) += 1; + } + + let mut ans = cnt.len() as i32; + + for i in k as usize..n { + *cnt.entry(candies[i - k as usize]).or_insert(0) += 1; + if let Some(x) = cnt.get_mut(&candies[i]) { + *x -= 1; + if *x == 0 { + cnt.remove(&candies[i]); + } + } + + ans = ans.max(cnt.len() as i32); + } + + ans + } +} diff --git a/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.ts b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.ts new file mode 100644 index 0000000000000..2c03a5bee8652 --- /dev/null +++ b/solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.ts @@ -0,0 +1,16 @@ +function shareCandies(candies: number[], k: number): number { + const cnt: Map = new Map(); + for (const x of candies.slice(k)) { + cnt.set(x, (cnt.get(x) || 0) + 1); + } + let ans = cnt.size; + for (let i = k; i < candies.length; ++i) { + cnt.set(candies[i - k], (cnt.get(candies[i - k]) || 0) + 1); + cnt.set(candies[i], (cnt.get(candies[i]) || 0) - 1); + if (cnt.get(candies[i]) === 0) { + cnt.delete(candies[i]); + } + ans = Math.max(ans, cnt.size); + } + return ans; +}