Ctrl K

Fresh Trays for Every Party

Hard
A bakery bakes buns in trays of tray_size and never starts a new tray before the previous one is sold out. Several parties of customers will come today: groups[i] is the size of the i-th party. Every customer takes exactly one bun, and a party is served completely before the next one. A party is happy if its first customer gets a bun from a fresh tray, meaning nothing was left over from earlier customers. You choose the order in which parties are served. Return the largest possible number of happy parties.
Example 1
tray_size=4, groups=[4, 1, 3, 2, 2] → 3

Order them 4, 1, 3, 2, 2: the group of 4 takes a whole tray, 1 + 3 empties the next one, and so does 2 + 2. The groups of 4, 1 and the first 2 are happy, 3 in total.

Example 2
tray_size=5, groups=[3, 3, 3] → 1

Only the first group starts on a fresh tray: 2 buns are left after it, and 4 after the second group.

Example 3
tray_size=1, groups=[7, 2, 5] → 3
Constraints
  • 1 ≤ tray_size ≤ 9
  • 1 ≤ len(groups) ≤ 30
  • 1 ≤ groups[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCheapest Basket with Bundles MediumGroups With Equal Sums MediumSettle Up in the Fewest Transfers HardTwo Groups With the Same Average HardAssembling Fragments Hard
loading Python…
Input
tray_size=4, groups=[4, 1, 3, 2, 2]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 127 tests on the server, hidden ones included.