1090.cpp (689B)
1 class Solution { 2 public: 3 int largestValsFromLabels(const vector<int> &values, const vector<int> &labels, int numWanted, 4 int useLimit) { 5 static int count[20001], idx[20001]; 6 memset(count, 0x00, sizeof(count)); 7 8 const int n = values.size(); 9 iota(begin(idx), begin(idx) + n, 0); 10 sort(begin(idx), begin(idx) + n, [&](int a, int b) { return values[a] > values[b]; }); 11 12 int res = 0; 13 for (const int i : idx) { 14 if (count[labels[i]] >= useLimit) continue; 15 res += values[i]; 16 if (!--numWanted) break; 17 count[labels[i]]++; 18 } 19 return res; 20 } 21 };