Ctrl K

The First Pack Size That Fits

Medium
A warehouse has received several batches of goods: batch i holds batches[i] items. There is also a list of packing options pack_sizes: option j packs goods into boxes of exactly pack_sizes[j] items each. A batch can be packed with option j only if its size is divisible by pack_sizes[j], with no partly filled box left over. For every batch, pick the fitting option with the smallest index. If no option fits, write -1. Return an array whose i-th element is the option chosen for batch i. The same option may be used for any number of batches.
Example 1
batches=[12, 9, 7, 6], pack_sizes=[6, 4, 3] → [0, 2, -1, 0]

12 is divisible by 6, so pack size 0. 9 is not divisible by 6 or 4 but is divisible by 3, so pack size 2. 7 fits none, so -1. 6 is divisible by 6, so pack size 0.

Example 2
batches=[5, 10, 15], pack_sizes=[10, 5, 1] → [1, 0, 1]

For 10, all of 10, 5 and 1 fit, and the earliest one has index 0. For 5 and 15, the first fitting size is 5, at index 1.

Constraints
  • 1 ≤ len(batches) ≤ 10⁵
  • 1 ≤ len(pack_sizes) ≤ 10⁵
  • 1 ≤ batches[i] ≤ 10⁵
  • 1 ≤ pack_sizes[j] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSudoku Board Without Clashes MediumAnagram Buckets MediumZero Out the Crosses MediumTree From Pre-Order and In-Order Walks MediumTree From In-Order and Post-Order Walks Medium
loading Python…
Input
batches=[12, 9, 7, 6], pack_sizes=[6, 4, 3]
Expected
[0, 2, -1, 0]

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