Skip to content

Commit 1d03be1

Browse files
committedDec 20, 2021
Working O(n)
1 parent cdd58ff commit 1d03be1

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed
 

‎.DS_Store

6 KB
Binary file not shown.

‎LeetCode/maximumUnitsOnATruck.cpp

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1-
2-
3-
First Code to commit.
1+
class Solution {
2+
public:
3+
4+
bool static sortcol(const vector<int> &v1,const vector<int> &v2)
5+
{
6+
return v1[1] > v2[1];
7+
}
8+
int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {
9+
sort(boxTypes.begin(),boxTypes.end(),sortcol);
10+
11+
12+
int ans = 0;
13+
for(int i = 0; i < boxTypes.size() && truckSize > 0; i++)
14+
{
15+
if(truckSize >= boxTypes[i][0])
16+
{
17+
ans = ans + boxTypes[i][1]*boxTypes[i][0];
18+
truckSize -= boxTypes[i][0];
19+
}
20+
else
21+
{
22+
ans = ans + boxTypes[i][1]*truckSize;
23+
truckSize = 0;
24+
}
25+
}
26+
27+
return ans;
28+
}
29+
};

0 commit comments

Comments
 (0)
Please sign in to comment.