地址:

5641. 卡车上的最大单元数

状态:AC

代码:

  1. class Solution {
  2. public:
  3. static bool cmp(vector<int> &a, vector<int> &b){
  4. return a[1] > b[1];
  5. }
  6. int maximumUnits(vector<vector<int>>& boxTypes, int truckSize) {
  7. int ans = 0;
  8. sort(boxTypes.begin(),boxTypes.end(),cmp);
  9. for(auto item:boxTypes){
  10. if(truckSize >= item[0]){
  11. truckSize -= item[0];
  12. ans = ans + item[0] * item[1];
  13. }else{
  14. ans = ans + truckSize * item[1];
  15. break;
  16. }
  17. }
  18. return ans;
  19. }
  20. };