Skip to content

1815. Maximum Number of Groups Getting Fresh Donuts

Description

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.

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.

You can freely rearrange the ordering of the groups. Return the maximum possible number of happy groups after rearranging the groups.

 

Example 1:

Input: batchSize = 3, groups = [1,2,3,4,5,6]
Output: 4
Explanation: You can arrange the groups as [6,2,4,5,1,3]. Then the 1st, 2nd, 4th, and 6th groups will be happy.

Example 2:

Input: batchSize = 4, groups = [1,3,2,5,2,2,1,6]
Output: 4

 

Constraints:

  • 1 <= batchSize <= 9
  • 1 <= groups.length <= 30
  • 1 <= groups[i] <= 109

 

Solutions

Solution: Dynamic Programming

  • Time complexity: O(nbatchSize)
  • Space complexity: O(nbatchSize)

 

JavaScript

js
/**
 * @param {number} batchSize
 * @param {number[]} groups
 * @return {number}
 */
const maxHappyGroups = function (batchSize, groups) {
  const customers = Array.from({ length: batchSize }, () => 0);
  const memo = new Map();
  let happyGroups = 0;

  for (const customer of groups) {
    const leftOver = customer % batchSize;

    if (leftOver) {
      const other = batchSize - leftOver;

      if (customers[other]) {
        customers[other] -= 1;
        happyGroups += 1;
      } else {
        customers[leftOver] += 1;
      }
    } else {
      happyGroups += 1;
    }
  }

  const getHappyGroups = leftOver => {
    const key = `${customers.join(',')},${leftOver}`;

    if (memo.has(key)) return memo.get(key);
    let result = 0;

    for (let index = 1; index < batchSize; index++) {
      if (!customers[index]) continue;
      const nextLeftOver = (leftOver + index) % batchSize;

      customers[index] -= 1;
      const happyGroups = getHappyGroups(nextLeftOver);
      const totalHappyGroups = leftOver ? happyGroups : happyGroups + 1;

      customers[index] += 1;
      result = Math.max(totalHappyGroups, result);
    }

    memo.set(key, result);

    return result;
  };

  return happyGroups + getHappyGroups(0);
};

Released under the MIT license