Pack Boxes Through a One-Sided Warehouse

Quick Overview

Pack Boxes Through a One-Sided Warehouse. boxes gives the heights of unit-width boxes, and warehouse gives the room heights from left to right. You may choose the boxes’ insertion order.

Pack Boxes Through a One-Sided Warehouse

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem `boxes` gives the heights of unit-width boxes, and `warehouse` gives the room heights from left to right. You may choose the boxes’ insertion order. Every box enters from the left and cannot pass through a shorter room or a previously placed box. Boxes cannot be stacked. Return the maximum number that can be placed. ```cpp class Solution { public: int maxBoxesInWarehouse(vector<int>& boxes, vector<int>& warehouse); }; ``` ### Examples **Example 1:** ```text boxes = [2,4,1], warehouse = [3,2,5] Output: 2 Explanation: Push height 1 to the final room and height 2 to the middle room; height 4 cannot pass the entrance. ``` **Example 2:** ```text boxes = [3,3,2], warehouse = [4,4] Output: 2 Explanation: Only two rooms exist, and two of these boxes can occupy them. ``` ### Constraints - `n == warehouse.length`. - Both array lengths are between `1` and `10^5`, inclusive. - All heights lie in `[1,10^9]`. ### Clarifying Questions The rooms are fixed, whereas box insertion order is free. The output is the maximum count, not a placement arrangement. ```hint Inspect the structure How can the entrance path and already placed boxes affect whether a proposed final arrangement is physically realizable? ``` ### Reasoning checkpoints - Show that every claimed placement can enter from the left without blockage or stacking. - Prove that no legal insertion order can place more boxes. ### Optional extensions How would allowing entry from both ends change each room’s accessibility?

Overview: Pack Boxes Through a One-Sided Warehouse. boxes gives the heights of unit-width boxes, and warehouse gives the room heights from left to right. You may choose the boxes’ insertion order.

Pinterest logo
Pinterest
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

boxes gives the heights of unit-width boxes, and warehouse gives the room heights from left to right. You may choose the boxes’ insertion order. Every box enters from the left and cannot pass through a shorter room or a previously placed box. Boxes cannot be stacked. Return the maximum number that can be placed.

class Solution {
public:
    int maxBoxesInWarehouse(vector<int>& boxes, vector<int>& warehouse);
};

Examples

Example 1:

boxes = [2,4,1], warehouse = [3,2,5]
Output: 2
Explanation: Push height 1 to the final room and height 2 to the middle room; height 4 cannot pass the entrance.

Example 2:

boxes = [3,3,2], warehouse = [4,4]
Output: 2
Explanation: Only two rooms exist, and two of these boxes can occupy them.

Constraints

  • n == warehouse.length .
  • Both array lengths are between 1 and 10^5 , inclusive.
  • All heights lie in [1,10^9] .

Clarifying Questions Guidance

The rooms are fixed, whereas box insertion order is free. The output is the maximum count, not a placement arrangement.

Reasoning checkpoints

  • Show that every claimed placement can enter from the left without blockage or stacking.
  • Prove that no legal insertion order can place more boxes.

Optional extensions

How would allowing entry from both ends change each room’s accessibility?