-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1710-maximum-units-on-a-truck.cpp
41 lines (38 loc) · 1.05 KB
/
1710-maximum-units-on-a-truck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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];
}
};
};