diff --git a/1710-maximum-units-on-a-truck/1710-maximum-units-on-a-truck.cpp b/1710-maximum-units-on-a-truck/1710-maximum-units-on-a-truck.cpp new file mode 100644 index 0000000..f1d9b2d --- /dev/null +++ b/1710-maximum-units-on-a-truck/1710-maximum-units-on-a-truck.cpp @@ -0,0 +1,41 @@ +class Solution +{ + public: + + int maximumUnits(vector> &boxTypes, int truckSize) + { + int res = 0; + + sort(boxTypes.begin(), boxTypes.end(), cmp()); + for (int i = 0; i < boxTypes.size(); i++) + { + if (truckSize != 0) + { + int currBoxes = boxTypes[i][0]; + int currBoxPerUnits = boxTypes[i][1]; + if (truckSize >= currBoxes) + { + truckSize -= currBoxes; + res += currBoxes * currBoxPerUnits; + } + else + { + res += truckSize * currBoxPerUnits; + truckSize = 0; + break; + } + } + } + + return res; + } + struct cmp + { + bool operator()(vector < int> + const &p1, vector < int> + const &p2) + { + return p1[1] > p2[1]; + } + }; +}; \ No newline at end of file