|
| 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> </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> </p> |
| 26 | +<p><strong>Constraints:</strong></p> |
| 27 | + |
| 28 | +<ul> |
| 29 | + <li><code>1 <= batchSize <= 9</code></li> |
| 30 | + <li><code>1 <= groups.length <= 30</code></li> |
| 31 | + <li><code>1 <= groups[i] <= 10<sup>9</sup></code></li> |
| 32 | +</ul> |
| 33 | +</div> |
| 34 | + |
| 35 | +<p> </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> </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 | +``` |
0 commit comments