Skip to content

Commit 8c6215e

Browse files
committed
feat: add solution 1815. Maximum Number of Groups Getting Fresh Donuts
1 parent d607678 commit 8c6215e

File tree

5 files changed

+156
-3
lines changed

5 files changed

+156
-3
lines changed

solutions/1807. Evaluate the Bracket Pairs of a String/solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
const evaluate = function (s, knowledge) {
77
const knowledgeMap = knowledge.reduce((map, [key, value]) => {
8-
return (map[key] = value), map;
8+
return ((map[key] = value), map);
99
}, {});
1010
let result = '';
1111
let key = '';

solutions/1814. Count Nice Pairs in an Array/solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const countNicePairs = function (nums) {
77
const uniqueNums = [...new Set(nums)];
88
const countMap = new Map();
99
const reverseMap = uniqueNums.reduce((map, num) => {
10-
return (map[num] = +`${num}`.split('').reverse().join('')), map;
10+
return ((map[num] = +`${num}`.split('').reverse().join('')), map);
1111
}, {});
1212
let result = 0;
1313

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# [1815. Maximum Number of Groups Getting Fresh Donuts](https://leetcode.com/problems/maximum-number-of-groups-getting-fresh-donuts)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>There is a donuts shop that bakes donuts in batches of <code>batchSize</code>. They have a rule where they must serve <strong>all</strong> of the donuts of a batch before serving any donuts of the next batch. You are given an integer <code>batchSize</code> and an integer array <code>groups</code>, where <code>groups[i]</code> denotes that there is a group of <code>groups[i]</code> customers that will visit the shop. Each customer will get exactly one donut.</p>
6+
7+
<p>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.</p>
8+
9+
<p>You can freely rearrange the ordering of the groups. Return <em>the <strong>maximum</strong> possible number of happy groups after rearranging the groups.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre><strong>Input:</strong> batchSize = 3, groups = [1,2,3,4,5,6]
15+
<strong>Output:</strong> 4
16+
<strong>Explanation:</strong> You can arrange the groups as [6,2,4,5,1,3]. Then the 1<sup>st</sup>, 2<sup>nd</sup>, 4<sup>th</sup>, and 6<sup>th</sup> groups will be happy.
17+
</pre>
18+
19+
<p><strong class="example">Example 2:</strong></p>
20+
21+
<pre><strong>Input:</strong> batchSize = 4, groups = [1,3,2,5,2,2,1,6]
22+
<strong>Output:</strong> 4
23+
</pre>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Constraints:</strong></p>
27+
28+
<ul>
29+
<li><code>1 &lt;= batchSize &lt;= 9</code></li>
30+
<li><code>1 &lt;= groups.length &lt;= 30</code></li>
31+
<li><code>1 &lt;= groups[i] &lt;= 10<sup>9</sup></code></li>
32+
</ul>
33+
</div>
34+
35+
<p>&nbsp;</p>
36+
37+
## Solutions
38+
39+
**Solution: `Dynamic Programming`**
40+
41+
- Time complexity: <em>O(n<sup>batchSize</sup>)</em>
42+
- Space complexity: <em>O(n<sup>batchSize</sup>)</em>
43+
44+
<p>&nbsp;</p>
45+
46+
### **JavaScript**
47+
48+
```js
49+
/**
50+
* @param {number} batchSize
51+
* @param {number[]} groups
52+
* @return {number}
53+
*/
54+
const maxHappyGroups = function (batchSize, groups) {
55+
const customers = Array.from({ length: batchSize }, () => 0);
56+
const memo = new Map();
57+
let happyGroups = 0;
58+
59+
for (const customer of groups) {
60+
const leftOver = customer % batchSize;
61+
62+
if (leftOver) {
63+
const other = batchSize - leftOver;
64+
65+
if (customers[other]) {
66+
customers[other] -= 1;
67+
happyGroups += 1;
68+
} else {
69+
customers[leftOver] += 1;
70+
}
71+
} else {
72+
happyGroups += 1;
73+
}
74+
}
75+
76+
const getHappyGroups = leftOver => {
77+
const key = `${customers.join(',')},${leftOver}`;
78+
79+
if (memo.has(key)) return memo.get(key);
80+
let result = 0;
81+
82+
for (let index = 1; index < batchSize; index++) {
83+
if (!customers[index]) continue;
84+
const nextLeftOver = (leftOver + index) % batchSize;
85+
86+
customers[index] -= 1;
87+
const happyGroups = getHappyGroups(nextLeftOver);
88+
const totalHappyGroups = leftOver ? happyGroups : happyGroups + 1;
89+
90+
customers[index] += 1;
91+
result = Math.max(totalHappyGroups, result);
92+
}
93+
94+
memo.set(key, result);
95+
96+
return result;
97+
};
98+
99+
return happyGroups + getHappyGroups(0);
100+
};
101+
```
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @param {number} batchSize
3+
* @param {number[]} groups
4+
* @return {number}
5+
*/
6+
const maxHappyGroups = function (batchSize, groups) {
7+
const customers = Array.from({ length: batchSize }, () => 0);
8+
const memo = new Map();
9+
let happyGroups = 0;
10+
11+
for (const customer of groups) {
12+
const leftOver = customer % batchSize;
13+
14+
if (leftOver) {
15+
const other = batchSize - leftOver;
16+
17+
if (customers[other]) {
18+
customers[other] -= 1;
19+
happyGroups += 1;
20+
} else {
21+
customers[leftOver] += 1;
22+
}
23+
} else {
24+
happyGroups += 1;
25+
}
26+
}
27+
28+
const getHappyGroups = leftOver => {
29+
const key = `${customers.join(',')},${leftOver}`;
30+
31+
if (memo.has(key)) return memo.get(key);
32+
let result = 0;
33+
34+
for (let index = 1; index < batchSize; index++) {
35+
if (!customers[index]) continue;
36+
const nextLeftOver = (leftOver + index) % batchSize;
37+
38+
customers[index] -= 1;
39+
const happyGroups = getHappyGroups(nextLeftOver);
40+
const totalHappyGroups = leftOver ? happyGroups : happyGroups + 1;
41+
42+
customers[index] += 1;
43+
result = Math.max(totalHappyGroups, result);
44+
}
45+
46+
memo.set(key, result);
47+
48+
return result;
49+
};
50+
51+
return happyGroups + getHappyGroups(0);
52+
};

solutions/1817. Finding the Users Active Minutes/solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
const findingUsersActiveMinutes = function (logs, k) {
77
const activeMap = logs.reduce((map, [id, time]) => {
8-
return (map[id] = (map[id] ?? new Set()).add(time)), map;
8+
return ((map[id] = (map[id] ?? new Set()).add(time)), map);
99
}, {});
1010
const result = new Array(k).fill(0);
1111
const actives = Object.values(activeMap);

0 commit comments

Comments
 (0)