nikunjsoni

1564

May 8th, 2021
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int maxBoxesInWarehouse(vector<int>& boxes, vector<int>& warehouse) {
  4.         // Sort boxes according to height.
  5.         sort(boxes.begin(), boxes.end());
  6.        
  7.         // Find min height box that can fit at each room.
  8.         for(int i=1; i<warehouse.size(); i++)
  9.             warehouse[i] = min(warehouse[i], warehouse[i-1]);
  10.        
  11.         // Fit the boxes.
  12.         int count = 0;
  13.         for(int i=warehouse.size()-1; i>=0; i--){
  14.             if(count < boxes.size() && boxes[count] <= warehouse[i])
  15.                 count++;
  16.         }
  17.         return count;        
  18.     }
  19. };
Advertisement
Add Comment
Please, Sign In to add comment