[LeetCode] Put Boxes Into the Warehouse I

1564. Put Boxes Into the Warehouse I

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int maxBoxesInWarehouse(vector<int>& boxes, vector<int>& warehouse) {
for(int i = 1; i < warehouse.size(); i++) {
if(warehouse[i] > warehouse[i - 1])
warehouse[i] = warehouse[i - 1];
}
sort(boxes.begin(), boxes.end());
int res = 0;
for(int i = warehouse.size() - 1; i >= 0 and res < boxes.size(); i--) {
if(warehouse[i] >= boxes[res])
res++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/21/PS/LeetCode/put-boxes-into-the-warehouse-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.