Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5af91fa

Browse files
authored
feat: add solutions to lc problem: No.2107 (doocs#1915)
No.2107.Number of Unique Flavors After Sharing K Candies
1 parent e5194b4 commit 5af91fa

File tree

8 files changed

+213
-61
lines changed

8 files changed

+213
-61
lines changed

solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README.md

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class Solution:
8383
cnt = Counter(candies[k:])
8484
ans = len(cnt)
8585
for i in range(k, len(candies)):
86-
cnt[candies[i]] -= 1
8786
cnt[candies[i - k]] += 1
87+
cnt[candies[i]] -= 1
8888
if cnt[candies[i]] == 0:
8989
cnt.pop(candies[i])
9090
ans = max(ans, len(cnt))
@@ -104,11 +104,11 @@ class Solution {
104104
cnt.merge(candies[i], 1, Integer::sum);
105105
}
106106
int ans = cnt.size();
107-
for (int i = k; i < candies.length; ++i) {
107+
for (int i = k; i < n; ++i) {
108+
cnt.merge(candies[i - k], 1, Integer::sum);
108109
if (cnt.merge(candies[i], -1, Integer::sum) == 0) {
109110
cnt.remove(candies[i]);
110111
}
111-
cnt.merge(candies[i - k], 1, Integer::sum);
112112
ans = Math.max(ans, cnt.size());
113113
}
114114
return ans;
@@ -128,11 +128,11 @@ public:
128128
++cnt[candies[i]];
129129
}
130130
int ans = cnt.size();
131-
for (int i = k; i < candies.size(); ++i) {
131+
for (int i = k; i < n; ++i) {
132+
++cnt[candies[i - k]];
132133
if (--cnt[candies[i]] == 0) {
133134
cnt.erase(candies[i]);
134135
}
135-
++cnt[candies[i - k]];
136136
ans = max(ans, (int) cnt.size());
137137
}
138138
return ans;
@@ -150,21 +150,69 @@ func shareCandies(candies []int, k int) (ans int) {
150150
}
151151
ans = len(cnt)
152152
for i := k; i < len(candies); i++ {
153+
cnt[candies[i-k]]++
153154
cnt[candies[i]]--
154155
if cnt[candies[i]] == 0 {
155156
delete(cnt, candies[i])
156157
}
157-
cnt[candies[i-k]]++
158158
ans = max(ans, len(cnt))
159159
}
160160
return
161161
}
162162
```
163163

164+
### **Rust**
165+
166+
```rust
167+
use std::collections::HashMap;
168+
169+
impl Solution {
170+
pub fn share_candies(candies: Vec<i32>, k: i32) -> i32 {
171+
let mut cnt = HashMap::new();
172+
let n = candies.len();
173+
174+
for i in k as usize..n {
175+
*cnt.entry(candies[i]).or_insert(0) += 1;
176+
}
177+
178+
let mut ans = cnt.len() as i32;
179+
180+
for i in k as usize..n {
181+
*cnt.entry(candies[i - k as usize]).or_insert(0) += 1;
182+
if let Some(x) = cnt.get_mut(&candies[i]) {
183+
*x -= 1;
184+
if *x == 0 {
185+
cnt.remove(&candies[i]);
186+
}
187+
}
188+
189+
ans = ans.max(cnt.len() as i32);
190+
}
191+
192+
ans
193+
}
194+
}
195+
```
196+
164197
### **TypeScript**
165198

166199
```ts
167-
200+
function shareCandies(candies: number[], k: number): number {
201+
const cnt: Map<number, number> = new Map();
202+
for (const x of candies.slice(k)) {
203+
cnt.set(x, (cnt.get(x) || 0) + 1);
204+
}
205+
let ans = cnt.size;
206+
for (let i = k; i < candies.length; ++i) {
207+
cnt.set(candies[i - k], (cnt.get(candies[i - k]) || 0) + 1);
208+
cnt.set(candies[i], (cnt.get(candies[i]) || 0) - 1);
209+
if (cnt.get(candies[i]) === 0) {
210+
cnt.delete(candies[i]);
211+
}
212+
ans = Math.max(ans, cnt.size);
213+
}
214+
return ans;
215+
}
168216
```
169217

170218
### **...**

solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/README_EN.md

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ There are 3 unique flavors, so return 3.
5454

5555
## Solutions
5656

57+
**Solution 1: Sliding Window + Hash Table**
58+
59+
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.
60+
61+
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()$.
62+
63+
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())$.
64+
65+
After traversing all the candies, we can get the maximum number of unique flavors of candies that can be retained.
66+
67+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of candies.
68+
5769
<!-- tabs:start -->
5870

5971
### **Python3**
@@ -64,8 +76,8 @@ class Solution:
6476
cnt = Counter(candies[k:])
6577
ans = len(cnt)
6678
for i in range(k, len(candies)):
67-
cnt[candies[i]] -= 1
6879
cnt[candies[i - k]] += 1
80+
cnt[candies[i]] -= 1
6981
if cnt[candies[i]] == 0:
7082
cnt.pop(candies[i])
7183
ans = max(ans, len(cnt))
@@ -83,11 +95,11 @@ class Solution {
8395
cnt.merge(candies[i], 1, Integer::sum);
8496
}
8597
int ans = cnt.size();
86-
for (int i = k; i < candies.length; ++i) {
98+
for (int i = k; i < n; ++i) {
99+
cnt.merge(candies[i - k], 1, Integer::sum);
87100
if (cnt.merge(candies[i], -1, Integer::sum) == 0) {
88101
cnt.remove(candies[i]);
89102
}
90-
cnt.merge(candies[i - k], 1, Integer::sum);
91103
ans = Math.max(ans, cnt.size());
92104
}
93105
return ans;
@@ -107,11 +119,11 @@ public:
107119
++cnt[candies[i]];
108120
}
109121
int ans = cnt.size();
110-
for (int i = k; i < candies.size(); ++i) {
122+
for (int i = k; i < n; ++i) {
123+
++cnt[candies[i - k]];
111124
if (--cnt[candies[i]] == 0) {
112125
cnt.erase(candies[i]);
113126
}
114-
++cnt[candies[i - k]];
115127
ans = max(ans, (int) cnt.size());
116128
}
117129
return ans;
@@ -129,21 +141,69 @@ func shareCandies(candies []int, k int) (ans int) {
129141
}
130142
ans = len(cnt)
131143
for i := k; i < len(candies); i++ {
144+
cnt[candies[i-k]]++
132145
cnt[candies[i]]--
133146
if cnt[candies[i]] == 0 {
134147
delete(cnt, candies[i])
135148
}
136-
cnt[candies[i-k]]++
137149
ans = max(ans, len(cnt))
138150
}
139151
return
140152
}
141153
```
142154

155+
### **Rust**
156+
157+
```rust
158+
use std::collections::HashMap;
159+
160+
impl Solution {
161+
pub fn share_candies(candies: Vec<i32>, k: i32) -> i32 {
162+
let mut cnt = HashMap::new();
163+
let n = candies.len();
164+
165+
for i in k as usize..n {
166+
*cnt.entry(candies[i]).or_insert(0) += 1;
167+
}
168+
169+
let mut ans = cnt.len() as i32;
170+
171+
for i in k as usize..n {
172+
*cnt.entry(candies[i - k as usize]).or_insert(0) += 1;
173+
if let Some(x) = cnt.get_mut(&candies[i]) {
174+
*x -= 1;
175+
if *x == 0 {
176+
cnt.remove(&candies[i]);
177+
}
178+
}
179+
180+
ans = ans.max(cnt.len() as i32);
181+
}
182+
183+
ans
184+
}
185+
}
186+
```
187+
143188
### **TypeScript**
144189

145190
```ts
146-
191+
function shareCandies(candies: number[], k: number): number {
192+
const cnt: Map<number, number> = new Map();
193+
for (const x of candies.slice(k)) {
194+
cnt.set(x, (cnt.get(x) || 0) + 1);
195+
}
196+
let ans = cnt.size;
197+
for (let i = k; i < candies.length; ++i) {
198+
cnt.set(candies[i - k], (cnt.get(candies[i - k]) || 0) + 1);
199+
cnt.set(candies[i], (cnt.get(candies[i]) || 0) - 1);
200+
if (cnt.get(candies[i]) === 0) {
201+
cnt.delete(candies[i]);
202+
}
203+
ans = Math.max(ans, cnt.size);
204+
}
205+
return ans;
206+
}
147207
```
148208

149209
### **...**
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
class Solution {
2-
public:
3-
int shareCandies(vector<int>& candies, int k) {
4-
unordered_map<int, int> cnt;
5-
int n = candies.size();
6-
for (int i = k; i < n; ++i) {
7-
++cnt[candies[i]];
8-
}
9-
int ans = cnt.size();
10-
for (int i = k; i < candies.size(); ++i) {
11-
if (--cnt[candies[i]] == 0) {
12-
cnt.erase(candies[i]);
13-
}
14-
++cnt[candies[i - k]];
15-
ans = max(ans, (int) cnt.size());
16-
}
17-
return ans;
18-
}
1+
class Solution {
2+
public:
3+
int shareCandies(vector<int>& candies, int k) {
4+
unordered_map<int, int> cnt;
5+
int n = candies.size();
6+
for (int i = k; i < n; ++i) {
7+
++cnt[candies[i]];
8+
}
9+
int ans = cnt.size();
10+
for (int i = k; i < n; ++i) {
11+
++cnt[candies[i - k]];
12+
if (--cnt[candies[i]] == 0) {
13+
cnt.erase(candies[i]);
14+
}
15+
ans = max(ans, (int) cnt.size());
16+
}
17+
return ans;
18+
}
1919
};

solution/2100-2199/2107.Number of Unique Flavors After Sharing K Candies/Solution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ func shareCandies(candies []int, k int) (ans int) {
55
}
66
ans = len(cnt)
77
for i := k; i < len(candies); i++ {
8+
cnt[candies[i-k]]++
89
cnt[candies[i]]--
910
if cnt[candies[i]] == 0 {
1011
delete(cnt, candies[i])
1112
}
12-
cnt[candies[i-k]]++
1313
ans = max(ans, len(cnt))
1414
}
1515
return
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
class Solution {
2-
public int shareCandies(int[] candies, int k) {
3-
Map<Integer, Integer> cnt = new HashMap<>();
4-
int n = candies.length;
5-
for (int i = k; i < n; ++i) {
6-
cnt.merge(candies[i], 1, Integer::sum);
7-
}
8-
int ans = cnt.size();
9-
for (int i = k; i < candies.length; ++i) {
10-
if (cnt.merge(candies[i], -1, Integer::sum) == 0) {
11-
cnt.remove(candies[i]);
12-
}
13-
cnt.merge(candies[i - k], 1, Integer::sum);
14-
ans = Math.max(ans, cnt.size());
15-
}
16-
return ans;
17-
}
1+
class Solution {
2+
public int shareCandies(int[] candies, int k) {
3+
Map<Integer, Integer> cnt = new HashMap<>();
4+
int n = candies.length;
5+
for (int i = k; i < n; ++i) {
6+
cnt.merge(candies[i], 1, Integer::sum);
7+
}
8+
int ans = cnt.size();
9+
for (int i = k; i < n; ++i) {
10+
cnt.merge(candies[i - k], 1, Integer::sum);
11+
if (cnt.merge(candies[i], -1, Integer::sum) == 0) {
12+
cnt.remove(candies[i]);
13+
}
14+
ans = Math.max(ans, cnt.size());
15+
}
16+
return ans;
17+
}
1818
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
class Solution:
2-
def shareCandies(self, candies: List[int], k: int) -> int:
3-
cnt = Counter(candies[k:])
4-
ans = len(cnt)
5-
for i in range(k, len(candies)):
6-
cnt[candies[i]] -= 1
7-
cnt[candies[i - k]] += 1
8-
if cnt[candies[i]] == 0:
9-
cnt.pop(candies[i])
10-
ans = max(ans, len(cnt))
11-
return ans
1+
class Solution:
2+
def shareCandies(self, candies: List[int], k: int) -> int:
3+
cnt = Counter(candies[k:])
4+
ans = len(cnt)
5+
for i in range(k, len(candies)):
6+
cnt[candies[i - k]] += 1
7+
cnt[candies[i]] -= 1
8+
if cnt[candies[i]] == 0:
9+
cnt.pop(candies[i])
10+
ans = max(ans, len(cnt))
11+
return ans
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn share_candies(candies: Vec<i32>, k: i32) -> i32 {
5+
let mut cnt = HashMap::new();
6+
let n = candies.len();
7+
8+
for i in k as usize..n {
9+
*cnt.entry(candies[i]).or_insert(0) += 1;
10+
}
11+
12+
let mut ans = cnt.len() as i32;
13+
14+
for i in k as usize..n {
15+
*cnt.entry(candies[i - k as usize]).or_insert(0) += 1;
16+
if let Some(x) = cnt.get_mut(&candies[i]) {
17+
*x -= 1;
18+
if *x == 0 {
19+
cnt.remove(&candies[i]);
20+
}
21+
}
22+
23+
ans = ans.max(cnt.len() as i32);
24+
}
25+
26+
ans
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function shareCandies(candies: number[], k: number): number {
2+
const cnt: Map<number, number> = new Map();
3+
for (const x of candies.slice(k)) {
4+
cnt.set(x, (cnt.get(x) || 0) + 1);
5+
}
6+
let ans = cnt.size;
7+
for (let i = k; i < candies.length; ++i) {
8+
cnt.set(candies[i - k], (cnt.get(candies[i - k]) || 0) + 1);
9+
cnt.set(candies[i], (cnt.get(candies[i]) || 0) - 1);
10+
if (cnt.get(candies[i]) === 0) {
11+
cnt.delete(candies[i]);
12+
}
13+
ans = Math.max(ans, cnt.size);
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
 (0)