Leetcode 1564. Put Boxes Into the Warehouse I
You are given two arrays of positive integers,
boxes
andwarehouse
, representing the heights of some boxes of unit width and the heights ofn
rooms in a warehouse respectively. The warehouse's rooms are labelled from0
ton - 1
from left to right wherewarehouse[i]
(0-indexed) is the height of theith
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.
greedy solution:
- convert the warehouse height to useable height
- sort the box, start from the right of warehouse
code with python3
def maxBoxesInWarehouse(self, boxes: List[int], warehouse: List[int]) -> int: for i in range(1,len(warehouse)): warehouse[i] = min(warehouse[i - 1], warehouse[i]) boxes.sort() ans = 0 for room in reversed(warehouse): if ans < len(boxes) and boxes[ans] <= room: ans += 1 return ans