Skip to content

Commit aca34c1

Browse files
committed
feat: add solutions to lc problem: No.2107
No.2107.Number of Unique Flavors After Sharing K Candies
1 parent 41d5f60 commit aca34c1

File tree

6 files changed

+243
-4
lines changed

6 files changed

+243
-4
lines changed

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

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,116 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:滑动窗口 + 哈希表**
63+
64+
我们可以维护一个大小为 $k$ 的滑动窗口,窗口外的糖果为自己的,窗口内的 $k$ 个糖果分给妹妹和妈妈。我们可以用哈希表 $cnt$ 记录窗口外的糖果口味以及对应的数量。
65+
66+
初始时,哈希表 $cnt$ 中存储的是 $candies[k]$ 到 $candies[n-1]$ 的糖果口味以及对应的数量。此时糖果口味的种类数为哈希表 $cnt$ 的大小,即 $ans = cnt.size()$。
67+
68+
接下来,我们遍历 $[k,..n-1]$ 范围内的糖果,将当前糖果 $candies[i]$ 加入窗口内,同时将窗口左侧的糖果 $candies[i-k]$ 移出窗口外。然后我们更新哈希表 $cnt$,并且更新糖果口味的种类数 $ans$ 为 $max(ans, cnt.size())$。
69+
70+
遍历完所有糖果后,我们即可得到最多可保留的独特口味的糖果。
71+
72+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为糖果的数量。
73+
6274
<!-- tabs:start -->
6375

6476
### **Python3**
6577

6678
<!-- 这里可写当前语言的特殊实现逻辑 -->
6779

6880
```python
69-
81+
class Solution:
82+
def shareCandies(self, candies: List[int], k: int) -> int:
83+
cnt = Counter(candies[k:])
84+
ans = len(cnt)
85+
for i in range(k, len(candies)):
86+
cnt[candies[i]] -= 1
87+
cnt[candies[i - k]] += 1
88+
if cnt[candies[i]] == 0:
89+
cnt.pop(candies[i])
90+
ans = max(ans, len(cnt))
91+
return ans
7092
```
7193

7294
### **Java**
7395

7496
<!-- 这里可写当前语言的特殊实现逻辑 -->
7597

7698
```java
99+
class Solution {
100+
public int shareCandies(int[] candies, int k) {
101+
Map<Integer, Integer> cnt = new HashMap<>();
102+
int n = candies.length;
103+
for (int i = k; i < n; ++i) {
104+
cnt.merge(candies[i], 1, Integer::sum);
105+
}
106+
int ans = cnt.size();
107+
for (int i = k; i < candies.length; ++i) {
108+
if (cnt.merge(candies[i], -1, Integer::sum) == 0) {
109+
cnt.remove(candies[i]);
110+
}
111+
cnt.merge(candies[i - k], 1, Integer::sum);
112+
ans = Math.max(ans, cnt.size());
113+
}
114+
return ans;
115+
}
116+
}
117+
```
77118

119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
int shareCandies(vector<int>& candies, int k) {
125+
unordered_map<int, int> cnt;
126+
int n = candies.size();
127+
for (int i = k; i < n; ++i) {
128+
++cnt[candies[i]];
129+
}
130+
int ans = cnt.size();
131+
for (int i = k; i < candies.size(); ++i) {
132+
if (--cnt[candies[i]] == 0) {
133+
cnt.erase(candies[i]);
134+
}
135+
++cnt[candies[i - k]];
136+
ans = max(ans, (int) cnt.size());
137+
}
138+
return ans;
139+
}
140+
};
78141
```
79142
80-
### **TypeScript**
143+
### **Go**
144+
145+
```go
146+
func shareCandies(candies []int, k int) (ans int) {
147+
cnt := map[int]int{}
148+
for _, c := range candies[k:] {
149+
cnt[c]++
150+
}
151+
ans = len(cnt)
152+
for i := k; i < len(candies); i++ {
153+
cnt[candies[i]]--
154+
if cnt[candies[i]] == 0 {
155+
delete(cnt, candies[i])
156+
}
157+
cnt[candies[i-k]]++
158+
ans = max(ans, len(cnt))
159+
}
160+
return
161+
}
162+
163+
func max(a, b int) int {
164+
if a > b {
165+
return a
166+
}
167+
return b
168+
}
169+
```
81170

82-
<!-- 这里可写当前语言的特殊实现逻辑 -->
171+
### **TypeScript**
83172

84173
```ts
85174

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

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,92 @@ There are 3 unique flavors, so return 3.
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def shareCandies(self, candies: List[int], k: int) -> int:
64+
cnt = Counter(candies[k:])
65+
ans = len(cnt)
66+
for i in range(k, len(candies)):
67+
cnt[candies[i]] -= 1
68+
cnt[candies[i - k]] += 1
69+
if cnt[candies[i]] == 0:
70+
cnt.pop(candies[i])
71+
ans = max(ans, len(cnt))
72+
return ans
6373
```
6474

6575
### **Java**
6676

6777
```java
78+
class Solution {
79+
public int shareCandies(int[] candies, int k) {
80+
Map<Integer, Integer> cnt = new HashMap<>();
81+
int n = candies.length;
82+
for (int i = k; i < n; ++i) {
83+
cnt.merge(candies[i], 1, Integer::sum);
84+
}
85+
int ans = cnt.size();
86+
for (int i = k; i < candies.length; ++i) {
87+
if (cnt.merge(candies[i], -1, Integer::sum) == 0) {
88+
cnt.remove(candies[i]);
89+
}
90+
cnt.merge(candies[i - k], 1, Integer::sum);
91+
ans = Math.max(ans, cnt.size());
92+
}
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int shareCandies(vector<int>& candies, int k) {
104+
unordered_map<int, int> cnt;
105+
int n = candies.size();
106+
for (int i = k; i < n; ++i) {
107+
++cnt[candies[i]];
108+
}
109+
int ans = cnt.size();
110+
for (int i = k; i < candies.size(); ++i) {
111+
if (--cnt[candies[i]] == 0) {
112+
cnt.erase(candies[i]);
113+
}
114+
++cnt[candies[i - k]];
115+
ans = max(ans, (int) cnt.size());
116+
}
117+
return ans;
118+
}
119+
};
120+
```
68121
122+
### **Go**
123+
124+
```go
125+
func shareCandies(candies []int, k int) (ans int) {
126+
cnt := map[int]int{}
127+
for _, c := range candies[k:] {
128+
cnt[c]++
129+
}
130+
ans = len(cnt)
131+
for i := k; i < len(candies); i++ {
132+
cnt[candies[i]]--
133+
if cnt[candies[i]] == 0 {
134+
delete(cnt, candies[i])
135+
}
136+
cnt[candies[i-k]]++
137+
ans = max(ans, len(cnt))
138+
}
139+
return
140+
}
141+
142+
func max(a, b int) int {
143+
if a > b {
144+
return a
145+
}
146+
return b
147+
}
69148
```
70149

71150
### **TypeScript**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
}
19+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func shareCandies(candies []int, k int) (ans int) {
2+
cnt := map[int]int{}
3+
for _, c := range candies[k:] {
4+
cnt[c]++
5+
}
6+
ans = len(cnt)
7+
for i := k; i < len(candies); i++ {
8+
cnt[candies[i]]--
9+
if cnt[candies[i]] == 0 {
10+
delete(cnt, candies[i])
11+
}
12+
cnt[candies[i-k]]++
13+
ans = max(ans, len(cnt))
14+
}
15+
return
16+
}
17+
18+
func max(a, b int) int {
19+
if a > b {
20+
return a
21+
}
22+
return b
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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

0 commit comments

Comments
 (0)