-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1710_maximumUnits.cc
49 lines (44 loc) · 1.02 KB
/
Problem_1710_maximumUnits.cc
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
42
43
44
45
46
47
48
49
#include <algorithm>
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int maximumUnits(vector<vector<int>> &boxTypes, int truckSize)
{
int ans = 0;
std::sort(boxTypes.begin(), boxTypes.end(), [](vector<int> &a, vector<int> &b) { return a[1] > b[1]; });
for (int i = 0; i < boxTypes.size(); i++)
{
// cout << boxTypes[i][0] << " " << boxTypes[i][1] << endl;
if (truckSize - boxTypes[i][0] >= 0)
{
ans += boxTypes[i][0] * boxTypes[i][1];
truckSize -= boxTypes[i][0];
}
else
{
ans += truckSize * boxTypes[i][1];
truckSize -= truckSize;
break;
}
}
return ans;
}
};
void testMaximumUnits()
{
Solution s;
vector<vector<int>> n1 = {{1, 3}, {2, 2}, {3, 1}};
vector<vector<int>> n2 = {{5, 10}, {2, 5}, {4, 7}, {3, 9}};
EXPECT_EQ_INT(8, s.maximumUnits(n1, 4));
EXPECT_EQ_INT(91, s.maximumUnits(n2, 10));
EXPECT_SUMMARY;
}
int main()
{
testMaximumUnits();
return 0;
}