Skip to content

Commit b2b31da

Browse files
committed
+ problem 1815
1 parent dde04b2 commit b2b31da

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 1815. Maximum Number of Groups Getting Fresh Donuts
2+
There is a donuts shop that bakes donuts in batches of `batchSize`. They have a rule where they must serve **all** of the donuts of a batch before serving any donuts of the next batch. You are given an integer `batchSize` and an integer array `groups`, where `groups[i]` denotes that there is a group of `groups[i]` customers that will visit the shop. Each customer will get exactly one donut.
3+
4+
When a group visits the shop, all customers of the group must be served before serving any of the following groups. A group will be happy if they all get fresh donuts. That is, the first customer of the group does not receive a donut that was left over from the previous group.
5+
6+
You can freely rearrange the ordering of the groups. Return *the **maximum** possible number of happy groups after rearranging the groups*.
7+
8+
#### Example 1:
9+
<pre>
10+
<strong>Input:</strong> batchSize = 3, groups = [1,2,3,4,5,6]
11+
<strong>Output:</strong> 4
12+
<strong>Explanation:</strong> You can arrange the groups as [6,2,4,5,1,3]. Then the 1st, 2nd, 4th, and 6th groups will be happy.
13+
</pre>
14+
15+
#### Example 2:
16+
<pre>
17+
<strong>Input:</strong> batchSize = 4, groups = [1,3,2,5,2,2,1,6]
18+
<strong>Output:</strong> 4
19+
</pre>
20+
21+
#### Constraints:
22+
* `1 <= batchSize <= 9`
23+
* `1 <= groups.length <= 30`
24+
* <code>1 <= groups[i] <= 10<sup>9</sup></code>
25+
26+
## Solutions (Python)
27+
28+
### 1. Solution
29+
```Python
30+
class Solution:
31+
def maxHappyGroups(self, batchSize: int, groups: List[int]) -> int:
32+
@cache
33+
def dfs(count: Tuple[int]) -> int:
34+
count = list(count)
35+
remain = (total - sum(i * count[i]
36+
for i in range(1, batchSize))) % batchSize
37+
ret = 0
38+
39+
for i in range(1, batchSize):
40+
if count[i] == 0:
41+
continue
42+
43+
count[i] -= 1
44+
if remain == 0:
45+
ret = max(ret, 1 + dfs(tuple(count)))
46+
else:
47+
ret = max(ret, dfs(tuple(count)))
48+
count[i] += 1
49+
50+
return ret
51+
52+
count = [0] * batchSize
53+
total = 0
54+
55+
for x in groups:
56+
count[x % batchSize] += 1
57+
total += x % batchSize
58+
59+
return count[0] + dfs(tuple(count))
60+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 1815. 得到新鲜甜甜圈的最多组数
2+
有一个甜甜圈商店,每批次都烤 `batchSize` 个甜甜圈。这个店铺有个规则,就是在烤一批新的甜甜圈时,之前 **所有** 甜甜圈都必须已经全部销售完毕。给你一个整数 `batchSize` 和一个整数数组 `groups` ,数组中的每个整数都代表一批前来购买甜甜圈的顾客,其中 `groups[i]` 表示这一批顾客的人数。每一位顾客都恰好只要一个甜甜圈。
3+
4+
当有一批顾客来到商店时,他们所有人都必须在下一批顾客来之前购买完甜甜圈。如果一批顾客中第一位顾客得到的甜甜圈不是上一组剩下的,那么这一组人都会很开心。
5+
6+
你可以随意安排每批顾客到来的顺序。请你返回在此前提下,**最多** 有多少组人会感到开心。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> batchSize = 3, groups = [1,2,3,4,5,6]
11+
<strong>输出:</strong> 4
12+
<strong>解释:</strong> 你可以将这些批次的顾客顺序安排为 [6,2,4,5,1,3] 。那么第 1,2,4,6 组都会感到开心。
13+
</pre>
14+
15+
#### 示例 2:
16+
<pre>
17+
<strong>输入:</strong> batchSize = 4, groups = [1,3,2,5,2,2,1,6]
18+
<strong>输出:</strong> 4
19+
</pre>
20+
21+
#### 提示:
22+
* `1 <= batchSize <= 9`
23+
* `1 <= groups.length <= 30`
24+
* <code>1 <= groups[i] <= 10<sup>9</sup></code>
25+
26+
## 题解 (Python)
27+
28+
### 1. 题解
29+
```Python
30+
class Solution:
31+
def maxHappyGroups(self, batchSize: int, groups: List[int]) -> int:
32+
@cache
33+
def dfs(count: Tuple[int]) -> int:
34+
count = list(count)
35+
remain = (total - sum(i * count[i]
36+
for i in range(1, batchSize))) % batchSize
37+
ret = 0
38+
39+
for i in range(1, batchSize):
40+
if count[i] == 0:
41+
continue
42+
43+
count[i] -= 1
44+
if remain == 0:
45+
ret = max(ret, 1 + dfs(tuple(count)))
46+
else:
47+
ret = max(ret, dfs(tuple(count)))
48+
count[i] += 1
49+
50+
return ret
51+
52+
count = [0] * batchSize
53+
total = 0
54+
55+
for x in groups:
56+
count[x % batchSize] += 1
57+
total += x % batchSize
58+
59+
return count[0] + dfs(tuple(count))
60+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def maxHappyGroups(self, batchSize: int, groups: List[int]) -> int:
3+
@cache
4+
def dfs(count: Tuple[int]) -> int:
5+
count = list(count)
6+
remain = (total - sum(i * count[i]
7+
for i in range(1, batchSize))) % batchSize
8+
ret = 0
9+
10+
for i in range(1, batchSize):
11+
if count[i] == 0:
12+
continue
13+
14+
count[i] -= 1
15+
if remain == 0:
16+
ret = max(ret, 1 + dfs(tuple(count)))
17+
else:
18+
ret = max(ret, dfs(tuple(count)))
19+
count[i] += 1
20+
21+
return ret
22+
23+
count = [0] * batchSize
24+
total = 0
25+
26+
for x in groups:
27+
count[x % batchSize] += 1
28+
total += x % batchSize
29+
30+
return count[0] + dfs(tuple(count))

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,7 @@
13251325
[1812][1812l]|[Determine Color of a Chessboard Square][1812] |![py]&nbsp;&nbsp;![rb]
13261326
[1813][1813l]|[Sentence Similarity III][1813] |![rs]
13271327
[1814][1814l]|[Count Nice Pairs in an Array][1814] |![rs]
1328+
[1815][1815l]|[Maximum Number of Groups Getting Fresh Donuts][1815] |![py]
13281329
[1816][1816l]|[Truncate Sentence][1816] |![py]
13291330
[1817][1817l]|[Finding the Users Active Minutes][1817] |![rs]
13301331
[1818][1818l]|[Minimum Absolute Sum Difference][1818] |![rs]
@@ -3266,6 +3267,7 @@
32663267
[1812]:Problemset/1812-Determine%20Color%20of%20a%20Chessboard%20Square/README.md#1812-determine-color-of-a-chessboard-square
32673268
[1813]:Problemset/1813-Sentence%20Similarity%20III/README.md#1813-sentence-similarity-iii
32683269
[1814]:Problemset/1814-Count%20Nice%20Pairs%20in%20an%20Array/README.md#1814-count-nice-pairs-in-an-array
3270+
[1815]:Problemset/1815-Maximum%20Number%20of%20Groups%20Getting%20Fresh%20Donuts/README.md#1815-maximum-number-of-groups-getting-fresh-donuts
32693271
[1816]:Problemset/1816-Truncate%20Sentence/README.md#1816-truncate-sentence
32703272
[1817]:Problemset/1817-Finding%20the%20Users%20Active%20Minutes/README.md#1817-finding-the-users-active-minutes
32713273
[1818]:Problemset/1818-Minimum%20Absolute%20Sum%20Difference/README.md#1818-minimum-absolute-sum-difference
@@ -5201,6 +5203,7 @@
52015203
[1812l]:https://leetcode.com/problems/determine-color-of-a-chessboard-square/
52025204
[1813l]:https://leetcode.com/problems/sentence-similarity-iii/
52035205
[1814l]:https://leetcode.com/problems/count-nice-pairs-in-an-array/
5206+
[1815l]:https://leetcode.com/problems/maximum-number-of-groups-getting-fresh-donuts/
52045207
[1816l]:https://leetcode.com/problems/truncate-sentence/
52055208
[1817l]:https://leetcode.com/problems/finding-the-users-active-minutes/
52065209
[1818l]:https://leetcode.com/problems/minimum-absolute-sum-difference/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,7 @@
13251325
[1812][1812l]|[判断国际象棋棋盘中一个格子的颜色][1812] |![py]&nbsp;&nbsp;![rb]
13261326
[1813][1813l]|[句子相似性 III][1813] |![rs]
13271327
[1814][1814l]|[统计一个数组中好对子的数目][1814] |![rs]
1328+
[1815][1815l]|[得到新鲜甜甜圈的最多组数][1815] |![py]
13281329
[1816][1816l]|[截断句子][1816] |![py]
13291330
[1817][1817l]|[查找用户活跃分钟数][1817] |![rs]
13301331
[1818][1818l]|[绝对差值和][1818] |![rs]
@@ -3266,6 +3267,7 @@
32663267
[1812]:Problemset/1812-Determine%20Color%20of%20a%20Chessboard%20Square/README_CN.md#1812-判断国际象棋棋盘中一个格子的颜色
32673268
[1813]:Problemset/1813-Sentence%20Similarity%20III/README_CN.md#1813-句子相似性-iii
32683269
[1814]:Problemset/1814-Count%20Nice%20Pairs%20in%20an%20Array/README_CN.md#1814-统计一个数组中好对子的数目
3270+
[1815]:Problemset/1815-Maximum%20Number%20of%20Groups%20Getting%20Fresh%20Donuts/README_CN.md#1815-得到新鲜甜甜圈的最多组数
32693271
[1816]:Problemset/1816-Truncate%20Sentence/README_CN.md#1816-截断句子
32703272
[1817]:Problemset/1817-Finding%20the%20Users%20Active%20Minutes/README_CN.md#1817-查找用户活跃分钟数
32713273
[1818]:Problemset/1818-Minimum%20Absolute%20Sum%20Difference/README_CN.md#1818-绝对差值和
@@ -5201,6 +5203,7 @@
52015203
[1812l]:https://leetcode.cn/problems/determine-color-of-a-chessboard-square/
52025204
[1813l]:https://leetcode.cn/problems/sentence-similarity-iii/
52035205
[1814l]:https://leetcode.cn/problems/count-nice-pairs-in-an-array/
5206+
[1815l]:https://leetcode.cn/problems/maximum-number-of-groups-getting-fresh-donuts/
52045207
[1816l]:https://leetcode.cn/problems/truncate-sentence/
52055208
[1817l]:https://leetcode.cn/problems/finding-the-users-active-minutes/
52065209
[1818l]:https://leetcode.cn/problems/minimum-absolute-sum-difference/

0 commit comments

Comments
 (0)