Skip to content

1090. Largest Values From Labels 👎

  • Time: $O(\texttt{sort})$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
 public:
  int largestValsFromLabels(vector<int>& values, vector<int>& labels,
                            int numWanted, int useLimit) {
    const int n = values.size();
    int ans = 0;
    vector<pair<int, int>> items;
    unordered_map<int, int> labelsUsed;

    for (int i = 0; i < n; ++i)
      items.emplace_back(values[i], labels[i]);

    ranges::sort(items, ranges::greater{},
                 [](const pair<int, int>& item) { return item.first; });

    for (const auto& [value, label] : items)
      if (labelsUsed[label] < useLimit) {
        ans += value;
        ++labelsUsed[label];
        if (--numWanted == 0)
          break;
      }

    return ans;
  }
};