aboutsummaryrefslogtreecommitdiff
path: root/max_units_truck.cpp
blob: f95e777f748feced74c125b4889c00fc775f59c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "leetcode.h"

class Solution {
public:
  int maximumUnits(vector<vector<int>> &boxTypes, int truckSize) {
    sort(boxTypes.begin(), boxTypes.end(), cmp);
    int ans = 0;
    for (auto box : boxTypes) {
      int n = min(box[0], truckSize);
      ans += (n * box[1]);
      truckSize -= n;
      if (!truckSize)
        break;
    }
    return ans;
  }

private:
  static bool cmp(vector<int> &a, vector<int> &b) { return a[1] > b[1]; }
};