Put Boxes Into the Warehouse I Problem
Description
LeetCode Problem 1564.
You are given two arrays of positive integers, boxes and warehouse, representing the heights of some boxes of unit width and the heights of n rooms in a warehouse respectively. The warehouse's rooms are labelled from 0 to n - 1 from left to right where warehouse[i] (0-indexed) is the height of the i^th room.
Boxes are put into the warehouse by the following rules:
- Boxes cannot be stacked.
- You can rearrange the insertion order of the boxes.
- Boxes can only be pushed into the warehouse from left to right only.
- If the height of some room in the warehouse is less than the height of a box, then that box and all other boxes behind it will be stopped before that room.
Return the maximum number of boxes you can put into the warehouse.
Example 1:

Input: boxes = [4,3,4,1], warehouse = [5,3,3,4,1]
Output: 3
Explanation:

We can first put the box of height 1 in room 4. Then we can put the box of height 3 in either of the 3 rooms 1, 2, or 3. Lastly, we can put one box of height 4 in room 0.
There is no way we can fit all 4 boxes in the warehouse.
Example 2:

Input: boxes = [1,2,2,3,4], warehouse = [3,4,1,2]
Output: 3
Explanation:

Notice that it's not possible to put the box of height 4 into the warehouse since it cannot pass the first room of height 3.
Also, for the last two rooms, 2 and 3, only boxes of height 1 can fit.
We can fit 3 boxes maximum as shown above. The yellow box can also be put in room 2 instead.
Swapping the orange and green boxes is also valid, or swapping one of them with the red box.
Example 3:
Input: boxes = [1,2,3], warehouse = [1,2,3,4]
Output: 1
Explanation: Since the first room in the warehouse is of height 1, we can only put boxes of height 1.
Constraints:
n == warehouse.length1 <= boxes.length, warehouse.length <= 10^51 <= boxes[i], warehouse[i] <= 10^9
Solution
Approach: Sort the Boxes, Fill From the Back
A box can only reach room i if it passes every room before it, so what really limits room i is the lowest room in warehouse[0..i]. Call that reach[i]. It never increases from left to right, so the deepest rooms are the most restrictive ones.
Once a room is taken, nothing can pass it, so the boxes have to be put in from the back to the front. That suggests a greedy order:
- Sort the boxes from smallest to largest.
- Keep a pointer
jat the deepest free room, starting atn - 1. - For each box, move
jleft past rooms withreach[j] < box, since they are too low for this box and for every bigger box after it. Ifjfalls off the front, stop. Otherwise put the box in roomjand movejone room left.
Why the smallest box goes deepest: the deep rooms have the smallest reach, so they can only be used by small boxes. A larger box placed there would take a room the small box needed, and the small box would also fit in any shallower room the large box could have used. So swapping never hurts, and the greedy count is the maximum.
Rooms skipped because they were too low stay empty. That is fine: no later, larger box could use them either.
Complexity: O(n + m log m) time for n rooms and m boxes (sorting the boxes), and O(n) extra space for reach.
class Solution:
def maxBoxesInWarehouse(self, boxes: List[int], warehouse: List[int]) -> int:
n = len(warehouse)
# reach[i]: the tallest box that can get to room i (the lowest room on the way)
reach = [warehouse[0]] * n
for i in range(1, n):
reach[i] = min(reach[i - 1], warehouse[i])
boxes.sort()
count, j = 0, n - 1
for box in boxes:
# the smallest remaining box goes into the deepest free room it can reach
while j >= 0 and reach[j] < box:
j -= 1
if j < 0:
break
count += 1
j -= 1
return count