Skip to content

Commit

Permalink
Time: 82 ms (49.08%), Space: 16 MB (55.90%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
ArbitCode committed Sep 2, 2022
1 parent aad4c30 commit dc570b7
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions 1710-maximum-units-on-a-truck/1710-maximum-units-on-a-truck.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution
{
public:

int maximumUnits(vector<vector < int>> &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];
}
};
};

0 comments on commit dc570b7

Please sign in to comment.