Ctrl K

Boxes Into a Two-Door Shed

Medium
A shed is a corridor of rooms, each one box wide, with a door at both ends. Room i (left to right, 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; - each box may be pushed in from either end; 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, 1, 4, 2] → 3

Box 5 goes in from the left into room 0. From the right, box 2 goes into room 3 and box 3 into room 2. Box 1 has nowhere left: room 1 can only be reached through occupied rooms. Three in total.

Example 2
boxes=[3, 3, 3], rooms=[2, 7, 2] → 0

Both entrances are only 2 high, so a box of height 3 cannot get in from either side.

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

Boxes of height 1 pass everywhere, so all three rooms get filled.

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, 1, 4, 2]
Expected
3

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