Ctrl K

Boxes Into a Dead-End Shed

Medium
A shed is a corridor of rooms, each one box wide, with a single entrance on the left. Room i (counting from the entrance, starting at 0) has height rooms[i]. You have unit-width boxes with heights boxes. The rules: - boxes cannot be stacked, and each room holds at most one box; - you choose the order in which the boxes go in; - a box is pushed from left to right; it cannot pass a room lower than itself, and then it and every box behind it stop in front of that room. Return the largest number of boxes you can fit into the shed.
Example 1
boxes=[2, 5, 3, 1], rooms=[6, 4, 4, 2] → 4

Push box 1 all the way to room 3, box 3 into room 2, box 2 into room 1, and box 5 stays in room 0 at the entrance. All four fit.

Example 2
boxes=[4, 4, 1], rooms=[3, 5, 5] → 1

The entrance room is only 3 high, so neither box of height 4 gets past it; only box 1 fits.

Example 3
boxes=[2, 2, 2], rooms=[9, 1, 9] → 1

A box of height 2 cannot pass room 1, so nothing can go beyond it. One box fits, in room 0.

Constraints
  • 1 ≤ len(boxes), len(rooms) ≤ 10⁵
  • 1 ≤ boxes[i], rooms[i] ≤ 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
boxes=[2, 5, 3, 1], rooms=[6, 4, 4, 2]
Expected
4

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