445. Put Boxes into the Warehouse II

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 labeled from 0 to n - 1 from left to right where warehouse[i] (0-indexed) is the height of the ith room.

Rules:

  1. Boxes cannot be stacked.
  2. You can rearrange the insertion order of the boxes.
  3. Boxes can be pushed into the warehouse from either left or right.
  4. If a room's height is less than a box's height, that box and any after it will be stopped before that room.

Return:

The maximum number of boxes that can be placed in the warehouse.

Example 1:

Input: boxes = [1, 2, 2, 3, 4], warehouse = [3, 4, 1, 2]

Output: 4

Explanation:

Boxes can be placed in the following order:

  • Box of height 1 in room 2 (left or right).
  • Box of height 2 in room 3 (right).
  • Box of height 2 in room 1 (left).
  • Box of height 3 in room 0 (left).

Example 2:

Input:boxes = [3, 5, 5, 2], warehouse = [2, 1, 3, 4, 5]

Output: 3

Explanation:

The two boxes of height 5 cannot fit in any room except one. A possible placement is:

  • Box of height 2 in room 2.
  • Box of height 3 in room 3.
  • Box of height 5 in room 4.

Now Your Turn!

Pick the correct output for the given input

Input:boxes = [4, 3, 1, 6], warehouse = [5, 3, 3, 2, 1]

Output: 3

Explanation:

A possible placement is:

  • Box of height 1 in room 4.
  • Box of height 3 in room 2.
  • Box of height 3 in room 1.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == warehouse.length
  • 1 <= boxes.length, warehouse.length <= 105
  • 1 <= boxes[i], warehouse[i] <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int maxBoxesInWarehouse(vector<int>& boxes, vector<int>& warehouse) {
// Your Code Goes Here
}
};
Test Case

Input:

Boxes
Warehouse