Put Boxes Into the Warehouse II Problem
Description
LeetCode Problem 1580.
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 labeled 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 be pushed into the warehouse from either side (left or right)
- 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 = [1,2,2,3,4], warehouse = [3,4,1,2]
Output: 4
Explanation:

We can store the boxes in the following order:
1- Put the yellow box in room 2 from either the left or right side.
2- Put the orange box in room 3 from the right side.
3- Put the green box in room 1 from the left side.
4- Put the red box in room 0 from the left side.
Notice that there are other valid ways to put 4 boxes such as swapping the red and green boxes or the red and orange boxes.
Example 2:

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

It is not possible to put the two boxes of height 5 in the warehouse since there's only 1 room of height >= 5.
Other valid solutions are to put the green box in room 2 or to put the orange box first in room 2 before putting the green and red boxes.
Constraints:
n == warehouse.length1 <= boxes.length, warehouse.length <= 10^51 <= boxes[i], warehouse[i] <= 10^9
Solution
Approach: Usable Heights, Then Greedy Matching
With two entrances, a box can reach room i from the left if it passes every room before i, or from the right if it passes every room after i. Let left[i] be the lowest room strictly to the left of i and right[i] the lowest room strictly to the right (infinity at the ends). The tallest box that can ever sit in room i is
usable[i] = min(warehouse[i], max(left[i], right[i]))
because the box must fit in the room itself and get through the better of the two ways in.
Now forget the positions: sort the usable heights and the boxes, and match them greedily. Walk the boxes from smallest to largest, and give each box the lowest remaining room it fits in. If no room is left, stop. Giving a small box the lowest room that works keeps the taller rooms for the taller boxes, so the number of matches is as large as possible.
Why the order of insertion does not spoil the matching: the rooms on the left side of the lowest room have usable heights that only go down as you move toward that lowest room, and the same holds on the right side. So on each side we can fill rooms from the middle outward (deepest first), and a box going to a deeper room never has to pass an occupied one. Every matching of boxes to rooms is therefore achievable.
Complexity: O(n log n + m log m) time to sort the rooms and the boxes, and O(n) extra space.
class Solution:
def maxBoxesInWarehouse(self, boxes: List[int], warehouse: List[int]) -> int:
n = len(warehouse)
# left[i] / right[i]: the lowest room strictly to the left / right of room i
left = [0] * n
right = [0] * n
left[0] = right[-1] = float("inf")
for i in range(1, n):
left[i] = min(left[i - 1], warehouse[i - 1])
for i in range(n - 2, -1, -1):
right[i] = min(right[i + 1], warehouse[i + 1])
# a room's usable height: its own height, capped by the better of the two ways in
rooms = sorted(min(warehouse[i], max(left[i], right[i])) for i in range(n))
boxes.sort()
count = i = 0
for box in boxes:
# the smallest remaining box takes the lowest room it fits in
while i < n and rooms[i] < box:
i += 1
if i == n:
break
count += 1
i += 1
return count