-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1710.cpp
32 lines (29 loc) · 848 Bytes
/
1710.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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Solution
{
public:
int maximumUnits(vector<vector<int>> &box, int truck) {
sort(box.begin(), box.end(), [](vector<int> &a, vector<int> &b) { return a[1] > b[1]; });
int sum = 0, idx = 0;
while (truck > 0) {
if (truck >= box[idx][0]) {
sum += box[idx][1] * box[idx][0];
truck -= box[idx][0];
} else {
sum += truck * box[idx][1];
truck = 0;
}
idx++;
}
return sum;
}
};
int main(int argc, char const *argv[]) {
Solution s;
vector<vector<int>> prob = {{1, 3}, {5, 5}, {2, 5}, {4, 2}, {4, 1}, {3, 1}, {2, 2}, {1, 3}, {2, 5}, {3, 2}};
cout << s.maximumUnits(prob, 35) << endl;
return 0;
}