Skip to main content
LeetCode 1564, Medium. Topics: Greedy, Array, Sorting. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:

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 i<sup>th</sup> 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 <em>the maximum number of boxes you can put into the warehouse.</em>

Examples

Example 1
Example 2

Constraints

  • n == warehouse.length
  • 1 <= boxes.length, warehouse.length <= 10^5
  • 1 <= boxes[i], warehouse[i] <= 10^9

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026