nikunjsoni

1710

Jun 14th, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {
  4.         sort(boxTypes.begin(), boxTypes.end(), Comparator());
  5.         int unitCount = 0;
  6.         for (auto boxType : boxTypes) {
  7.             int boxCount = min(truckSize, boxType[0]);
  8.             unitCount += boxCount * boxType[1];
  9.             truckSize -= boxCount;
  10.             if (truckSize == 0) break;
  11.         }
  12.         return unitCount;
  13.     }
  14.  
  15.     struct Comparator {
  16.         bool operator()(vector<int> const& p1, vector<int> const& p2) {
  17.             return p1[1] > p2[1];
  18.         }
  19.     };
  20. };
Advertisement
Add Comment
Please, Sign In to add comment