Skip to content

Commit 18eda61

Browse files
committed
Runtime: 87 ms (Top 39.73%) | Memory: 17 MB (Top 32.47%)
1 parent df11de7 commit 18eda61

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
// Runtime: 87 ms (Top 39.73%) | Memory: 17 MB (Top 32.47%)
12
class Solution {
23
public:
34
static bool myfunction(vector<int>& a, vector<int>& b){
45
return a[1] > b[1];
56
}
67
int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {
7-
//custom sort (in increasing order of numberOfUnitsPerBox as we have to return maximum total number of units )
8+
//custom sort (in increasing order of numberOfUnitsPerBox as we have to return maximum total number of units )
89
sort(boxTypes.begin(),boxTypes.end(),myfunction);
9-
//greedily pick boxes till capacity is full
10+
//greedily pick boxes till capacity is full
1011
int ans=0;
1112
for(auto box: boxTypes){
12-
int x=min(box[0],truckSize); //choose minimum boxes from available boxes and capacity left
13-
ans+=(x*box[1]); //adding units in ans
14-
truckSize-=x; //reduce the capacity
15-
if(!truckSize) break; //capacity full
13+
int x=min(box[0],truckSize); //choose minimum boxes from available boxes and capacity left
14+
ans+=(x*box[1]); //adding units in ans
15+
truckSize-=x; //reduce the capacity
16+
if(!truckSize) break; //capacity full
1617
}
1718
return ans;
1819
}
19-
};
20+
};

0 commit comments

Comments
 (0)