Skip to content

Commit 2da7999

Browse files
committed
feat: add solutions to lc problem: No.1815
No.1815.Maximum Number of Groups Getting Fresh Donuts
1 parent d421b15 commit 2da7999

File tree

7 files changed

+417
-14
lines changed

7 files changed

+417
-14
lines changed

solution/1300-1399/1323.Maximum 69 Number/README_EN.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ func maximum69Number(num int) int {
103103
}
104104
```
105105

106-
107106
### **TypeScript**
108107

109108
```ts

solution/1800-1899/1815.Maximum Number of Groups Getting Fresh Donuts/README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,52 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:贪心 + 状态压缩 + 记忆化搜索**
47+
48+
题目实际上要我们找到一种安排顺序,使得前缀和(这里指的是“人数”)与 $batchSize$ 取模后为 $0$ 的组数最多。因此,我们可以将所有顾客按组分成两类:
49+
50+
- 人数为 $batchSize$ 的整数倍的顾客,这些顾客不会对下一组顾客的甜甜圈产生影响,我们可以贪心地优先安排这些组的顾客,那么这些组的顾客都会感到开心,“初始答案”为这些组的数量;
51+
- 人数不为 $batchSize$ 的整数倍的顾客,这些顾客的安排顺序会影响下一组顾客的甜甜圈。我们可以对这里每一组的人数 $v$ 模 $batchSize$,得到的这些余数构成一个集合,集合中的元素值范围是 $[1,2...,batchSize-1]$。数组 $groups$ 的长度最大为 $30$,因此,每个余数的数量最大不超过 $30$。我们可以用 $5$ 个二进制位来表示一个余数的数量,而 $batchSize$ 最大为 $9$,那么表示这些余数以及对应的数量总共需要的二进制位就是 $5\times (9-1)=40$。我们可以用一个 $64$ 位整数 $state$ 来表示。
52+
53+
接下来,我们设计一个函数 $dfs(state, mod)$,表示安排状态为 $state$,且当前前缀余数为 $mod$ 时,能使得多少组感到开心。那么我们在“初始答案”加上 $dfs(state, 0)$,即为最终答案。
54+
55+
函数 $dfs(state, mod)$ 的实现逻辑如下:
56+
57+
我们枚举 $1$ 到 $batchSize-1$ 的每一个余数 $i$,如果余数 $i$ 的数量不为 $0$,那么我们可以将余数 $i$ 的数量减去 $1$,将当前前缀余数加上 $i$ 并且对 $batchSize$ 取模,然后递归调用函数 $dfs$,求出子状态的最优解,取最大值即可。最后判断 $mod$ 是否为 $0$,如果为 $0$,我们在最大值上加 $1$ 后返回,否则直接返回最大值。
58+
59+
过程中,我们可以使用记忆化搜索来避免状态的重复计算。
60+
61+
时间复杂度不超过 $O(10^7)$,空间复杂度不超过 $O(10^6)$。
62+
4663
<!-- tabs:start -->
4764

4865
### **Python3**
4966

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

69+
```python
70+
class Solution:
71+
def maxHappyGroups(self, batchSize: int, groups: List[int]) -> int:
72+
@cache
73+
def dfs(state, mod):
74+
res = 0
75+
x = int(mod == 0)
76+
for i in range(1, batchSize):
77+
if state >> (i * 5) & 31:
78+
t = dfs(state - (1 << (i * 5)), (mod + i) % batchSize)
79+
res = max(res, t + x)
80+
return res
81+
82+
state = ans = 0
83+
for v in groups:
84+
i = v % batchSize
85+
ans += i == 0
86+
if i:
87+
state += 1 << (i * 5)
88+
ans += dfs(state, 0)
89+
return ans
90+
```
91+
5292
```python
5393
class Solution:
5494
def maxHappyGroups(self, batchSize: int, groups: List[int]) -> int:
@@ -75,7 +115,123 @@ class Solution:
75115
<!-- 这里可写当前语言的特殊实现逻辑 -->
76116

77117
```java
118+
class Solution {
119+
private Map<Long, Integer> f = new HashMap<>();
120+
private int size;
121+
122+
public int maxHappyGroups(int batchSize, int[] groups) {
123+
size = batchSize;
124+
int ans = 0;
125+
long state = 0;
126+
for (int g : groups) {
127+
int i = g % size;
128+
if (i == 0) {
129+
++ans;
130+
} else {
131+
state += 1l << (i * 5);
132+
}
133+
}
134+
ans += dfs(state, 0);
135+
return ans;
136+
}
137+
138+
private int dfs(long state, int mod) {
139+
if (f.containsKey(state)) {
140+
return f.get(state);
141+
}
142+
int res = 0;
143+
for (int i = 1; i < size; ++i) {
144+
if ((state >> (i * 5) & 31) != 0) {
145+
int t = dfs(state - (1l << (i * 5)), (mod + i) % size);
146+
res = Math.max(res, t + (mod == 0 ? 1 : 0));
147+
}
148+
}
149+
f.put(state, res);
150+
return res;
151+
}
152+
}
153+
```
154+
155+
### **C++**
156+
157+
```cpp
158+
class Solution {
159+
public:
160+
int maxHappyGroups(int batchSize, vector<int>& groups) {
161+
using ll = long long;
162+
unordered_map<ll, int> f;
163+
ll state = 0;
164+
int ans = 0;
165+
for (auto& v : groups) {
166+
int i = v % batchSize;
167+
ans += i == 0;
168+
if (i) {
169+
state += 1ll << (i * 5);
170+
}
171+
}
172+
function<int(ll, int)> dfs = [&](ll state, int mod) {
173+
if (f.count(state)) {
174+
return f[state];
175+
}
176+
int res = 0;
177+
int x = mod == 0;
178+
for (int i = 1; i < batchSize; ++i) {
179+
if (state >> (i * 5) & 31) {
180+
int t = dfs(state - (1ll << (i * 5)), (mod + i) % batchSize);
181+
res = max(res, t + x);
182+
}
183+
}
184+
return f[state] = res;
185+
};
186+
ans += dfs(state, 0);
187+
return ans;
188+
}
189+
};
190+
```
78191
192+
### **Go**
193+
194+
```go
195+
func maxHappyGroups(batchSize int, groups []int) (ans int) {
196+
state := 0
197+
for _, v := range groups {
198+
i := v % batchSize
199+
if i == 0 {
200+
ans++
201+
} else {
202+
state += 1 << (i * 5)
203+
}
204+
}
205+
f := map[int]int{}
206+
var dfs func(int, int) int
207+
dfs = func(state, mod int) int {
208+
if v, ok := f[state]; ok {
209+
return v
210+
}
211+
res := 0
212+
x := 0
213+
if mod == 0 {
214+
x = 1
215+
}
216+
for i := 1; i < batchSize; i++ {
217+
if state>>(i*5)&31 != 0 {
218+
t := dfs(state-1<<(i*5), (mod+i)%batchSize)
219+
res = max(res, t+x)
220+
}
221+
}
222+
f[state] = res
223+
return res
224+
}
225+
ans += dfs(state, 0)
226+
return
227+
}
228+
229+
func max(a, b int) int {
230+
if a > b {
231+
return a
232+
}
233+
return b
234+
}
79235
```
80236

81237
### **...**

solution/1800-1899/1815.Maximum Number of Groups Getting Fresh Donuts/README_EN.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,29 @@
4141

4242
### **Python3**
4343

44+
```python
45+
class Solution:
46+
def maxHappyGroups(self, batchSize: int, groups: List[int]) -> int:
47+
@cache
48+
def dfs(state, mod):
49+
res = 0
50+
x = int(mod == 0)
51+
for i in range(1, batchSize):
52+
if state >> (i * 5) & 31:
53+
t = dfs(state - (1 << (i * 5)), (mod + i) % batchSize)
54+
res = max(res, t + x)
55+
return res
56+
57+
state = ans = 0
58+
for v in groups:
59+
i = v % batchSize
60+
ans += i == 0
61+
if i:
62+
state += 1 << (i * 5)
63+
ans += dfs(state, 0)
64+
return ans
65+
```
66+
4467
```python
4568
class Solution:
4669
def maxHappyGroups(self, batchSize: int, groups: List[int]) -> int:
@@ -65,7 +88,123 @@ class Solution:
6588
### **Java**
6689

6790
```java
91+
class Solution {
92+
private Map<Long, Integer> f = new HashMap<>();
93+
private int size;
94+
95+
public int maxHappyGroups(int batchSize, int[] groups) {
96+
size = batchSize;
97+
int ans = 0;
98+
long state = 0;
99+
for (int g : groups) {
100+
int i = g % size;
101+
if (i == 0) {
102+
++ans;
103+
} else {
104+
state += 1l << (i * 5);
105+
}
106+
}
107+
ans += dfs(state, 0);
108+
return ans;
109+
}
110+
111+
private int dfs(long state, int mod) {
112+
if (f.containsKey(state)) {
113+
return f.get(state);
114+
}
115+
int res = 0;
116+
for (int i = 1; i < size; ++i) {
117+
if ((state >> (i * 5) & 31) != 0) {
118+
int t = dfs(state - (1l << (i * 5)), (mod + i) % size);
119+
res = Math.max(res, t + (mod == 0 ? 1 : 0));
120+
}
121+
}
122+
f.put(state, res);
123+
return res;
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
int maxHappyGroups(int batchSize, vector<int>& groups) {
134+
using ll = long long;
135+
unordered_map<ll, int> f;
136+
ll state = 0;
137+
int ans = 0;
138+
for (auto& v : groups) {
139+
int i = v % batchSize;
140+
ans += i == 0;
141+
if (i) {
142+
state += 1ll << (i * 5);
143+
}
144+
}
145+
function<int(ll, int)> dfs = [&](ll state, int mod) {
146+
if (f.count(state)) {
147+
return f[state];
148+
}
149+
int res = 0;
150+
int x = mod == 0;
151+
for (int i = 1; i < batchSize; ++i) {
152+
if (state >> (i * 5) & 31) {
153+
int t = dfs(state - (1ll << (i * 5)), (mod + i) % batchSize);
154+
res = max(res, t + x);
155+
}
156+
}
157+
return f[state] = res;
158+
};
159+
ans += dfs(state, 0);
160+
return ans;
161+
}
162+
};
163+
```
68164
165+
### **Go**
166+
167+
```go
168+
func maxHappyGroups(batchSize int, groups []int) (ans int) {
169+
state := 0
170+
for _, v := range groups {
171+
i := v % batchSize
172+
if i == 0 {
173+
ans++
174+
} else {
175+
state += 1 << (i * 5)
176+
}
177+
}
178+
f := map[int]int{}
179+
var dfs func(int, int) int
180+
dfs = func(state, mod int) int {
181+
if v, ok := f[state]; ok {
182+
return v
183+
}
184+
res := 0
185+
x := 0
186+
if mod == 0 {
187+
x = 1
188+
}
189+
for i := 1; i < batchSize; i++ {
190+
if state>>(i*5)&31 != 0 {
191+
t := dfs(state-1<<(i*5), (mod+i)%batchSize)
192+
res = max(res, t+x)
193+
}
194+
}
195+
f[state] = res
196+
return res
197+
}
198+
ans += dfs(state, 0)
199+
return
200+
}
201+
202+
func max(a, b int) int {
203+
if a > b {
204+
return a
205+
}
206+
return b
207+
}
69208
```
70209

71210
### **...**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public:
3+
int maxHappyGroups(int batchSize, vector<int>& groups) {
4+
using ll = long long;
5+
unordered_map<ll, int> f;
6+
ll state = 0;
7+
int ans = 0;
8+
for (auto& v : groups) {
9+
int i = v % batchSize;
10+
ans += i == 0;
11+
if (i) {
12+
state += 1ll << (i * 5);
13+
}
14+
}
15+
function<int(ll, int)> dfs = [&](ll state, int mod) {
16+
if (f.count(state)) {
17+
return f[state];
18+
}
19+
int res = 0;
20+
int x = mod == 0;
21+
for (int i = 1; i < batchSize; ++i) {
22+
if (state >> (i * 5) & 31) {
23+
int t = dfs(state - (1ll << (i * 5)), (mod + i) % batchSize);
24+
res = max(res, t + x);
25+
}
26+
}
27+
return f[state] = res;
28+
};
29+
ans += dfs(state, 0);
30+
return ans;
31+
}
32+
};

0 commit comments

Comments
 (0)