File tree 1 file changed +8
-7
lines changed
scripts/algorithms/M/Maximum Units on a Truck 1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 87 ms (Top 39.73%) | Memory: 17 MB (Top 32.47%)
1
2
class Solution {
2
3
public:
3
4
static bool myfunction (vector<int >& a, vector<int >& b){
4
5
return a[1 ] > b[1 ];
5
6
}
6
7
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 )
8
9
sort (boxTypes.begin (),boxTypes.end (),myfunction);
9
- // greedily pick boxes till capacity is full
10
+ // greedily pick boxes till capacity is full
10
11
int ans=0 ;
11
12
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
16
17
}
17
18
return ans;
18
19
}
19
- };
20
+ };
You can’t perform that action at this time.
0 commit comments