classSolution { public: intlargestValsFromLabels(vector<int>& values, vector<int>& labels, int numWanted, int useLimit){ int n = values.size(); vector<int> id(n); iota(id.begin(), id.end(), 0); sort(id.begin(), id.end(), [&](int i, int j) { return values[i] > values[j]; });
int ans = 0, choose = 0; unordered_map<int, int> cnt; for (int i = 0; i < n && choose < numWanted; ++i) { int label = labels[id[i]]; if (cnt[label] == useLimit) { continue; } ++choose; ans += values[id[i]]; ++cnt[label]; } return ans; } };
publicclassSolution { publicintLargestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) { int n = values.Length; int[] id = newint[n]; for (int i = 0; i < n; i++) { id[i] = i; } Array.Sort(id, (a, b) => values[b] - values[a]);
int ans = 0, choose = 0; IDictionary<int, int> cnt = new Dictionary<int, int>(); for (int i = 0; i < n && choose < numWanted; ++i) { int label = labels[id[i]]; if (cnt.ContainsKey(label) && cnt[label] == useLimit) { continue; } ++choose; ans += values[id[i]]; cnt.TryAdd(label, 0); ++cnt[label]; } return ans; } }
ans = choose = 0 cnt = Counter() for i inrange(n): label = labels[idx[i]] if cnt[label] == useLimit: continue; choose += 1 ans += values[idx[i]] cnt[label] += 1 if choose == numWanted: break return ans
intlargestValsFromLabels(int* values, int valuesSize, int* labels, int labelsSize, int numWanted, int useLimit) { int n = valuesSize; int arr[n][2]; for (int i = 0; i < n; i++) { arr[i][0] = values[i]; arr[i][1] = i; } qsort(arr, n, sizeof(arr[0]), cmp);
int ans = 0, choose = 0; HashItem *cnt = NULL; for (int i = 0; i < n && choose < numWanted; ++i) { int id = arr[i][1]; int label = labels[id]; if (hashGetItem(&cnt, label, 0) == useLimit) { continue; } ++choose; ans += values[id]; hashSetItem(&cnt, label, hashGetItem(&cnt, label, 0) + 1); } hashFree(&cnt); return ans; }