5641. 卡车上的最大单元数
状态:AC
代码:
class Solution {
public:
static bool cmp(vector<int> &a, vector<int> &b){
return a[1] > b[1];
}
int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {
int ans = 0;
sort(boxTypes.begin(),boxTypes.end(),cmp);
for(auto item:boxTypes){
if(truckSize >= item[0]){
truckSize -= item[0];
ans = ans + item[0] * item[1];
}else{
ans = ans + truckSize * item[1];
break;
}
}
return ans;
}
};