Ctrl K

Fill the Boxes to the Brim

Medium
A warehouse has n boxes. Box i holds up to limits[i] items and already contains loaded[i]. You also have spare loose items that you can put into any boxes (you don't have to use them all). Return the largest number of boxes that can end up completely full.
Example 1
limits=[5, 3, 7, 2], loaded=[5, 1, 3, 0], spare=4 → 3

The boxes are short 0, 2, 4 and 2 items. The first one is already full, and two more are topped up with 2 items each, giving 3 full boxes.

Example 2
limits=[6, 6], loaded=[1, 2], spare=3 → 0

Each box needs more than 3 items, so none of them can be filled.

Constraints
  • n = len(limits) = len(loaded)
  • 1 ≤ n ≤ 5 · 10⁴
  • 1 ≤ limits[i] ≤ 10⁹
  • 0 ≤ loaded[i] ≤ limits[i]
  • 1 ≤ spare ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
limits=[5, 3, 7, 2], loaded=[5, 1, 3, 0], spare=4
Expected
3

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