Put Boxes Into the Warehouse I - Problem
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 ith 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.
Input & Output
Example 1 — Basic Case
$
Input:
boxes = [4,3,4,1], warehouse = [5,3,3,4,4]
›
Output:
3
💡 Note:
Effective heights are [5,3,3,3,3]. Sort boxes to [1,3,4,4]. Place box 1 at position 0, box 3 at position 1, box 4 cannot fit anywhere else. Total: 3 boxes.
Example 2 — Height Constraint
$
Input:
boxes = [1,2,2,3,4], warehouse = [3,4,1,2]
›
Output:
3
💡 Note:
Effective heights are [3,3,1,1]. Sorted boxes [1,2,2,3,4]. Place box 1 at pos 0, box 2 at pos 1, then remaining boxes too tall for remaining positions.
Example 3 — All Boxes Fit
$
Input:
boxes = [1,2,3], warehouse = [4,4,4]
›
Output:
3
💡 Note:
All warehouse positions have height 4, so all boxes [1,2,3] can be placed. Effective heights are [4,4,4].
Constraints
- n == boxes.length
- m == warehouse.length
- 1 ≤ n, m ≤ 105
- 1 ≤ boxes[i], warehouse[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Boxes [4,3,4,1] and Warehouse [5,3,3,4,4]
2
Process
Compute effective heights and sort boxes for optimal placement
3
Output
Maximum 3 boxes can be placed
Key Takeaway
🎯 Key Insight: The effective height at each position is the minimum height encountered from the left, creating a bottleneck effect
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code