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: 2
💡 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 at position 2 (height 3 < 4). Total: 2 boxes.
Example 2 — Height Constraint
$ Input: boxes = [1,2,2,3,4], warehouse = [3,4,1,2]
Output: 2
💡 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. Total: 2 boxes.
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
Put Boxes Into the Warehouse I INPUT Boxes = [4, 3, 4, 1] 4 3 4 1 Warehouse = [5, 3, 3, 4, 4] h=5 r0 h=3 r1 h=3 r2 h=4 r3 h=4 r4 Push from left --> ALGORITHM STEPS 1 Sort Boxes (desc) [4, 4, 3, 1] - largest first 2 Effective Heights min(h[i], effective[i-1]) Original: [5, 3, 3, 4, 4] Effective: [5, 3, 3, 3, 3] Blocked by room h=3 3 Greedy Match Fit largest box in rightmost room Box 4: skip (too tall) Box 4: skip (too tall) Box 3: fits room 4 [OK] Box 3: fits room 3 [OK] Box 1: fits room 2 [OK] 4 Count Placed Return total boxes placed FINAL RESULT Warehouse with boxes placed: empty empty box:1 box:3 box:3 Output 3 3 boxes placed Boxes [3,3,1] fit in rooms [4,3,2] [OK] Maximum achieved Key Insight: Calculate "effective heights" by scanning left-to-right: each room's effective height is min(current height, previous effective height). Sort boxes descending, then greedily place each box in the rightmost room it can fit. This ensures maximum boxes are placed. TutorialsPoint - Put Boxes Into the Warehouse I | Greedy with Effective Heights
Asked in
Amazon 35 Google 28
28.5K Views
Medium Frequency
~15 min Avg. Time
824 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen