Distribute Repeating Integers LeetCode solution in C++

1
class Solution {
2
public:
3
bool solve(vector<int> &q, map<int, int> &count, int idx) {
4
if (idx == q.size()) {
5
return true;
6
}
7
for (auto it = count.begin(); it != count.end(); it++) {
8
if (it->second >= q[idx]) {
9
count[it->first] -= q[idx];
10
if (solve(q, count, idx + 1)) return true;
11
count[it->first] += q[idx];
12
}
13
}
14
return false;
15
}
16
bool canDistribute(vector<int> &nums, vector<int> &quantity) {
17
map<int, int> count;
18
int n = nums.size();
19
for (int i = 0; i < n; i++) {
20
count[nums[i]]++;
21
}
22
sort(quantity.begin(), quantity.end(), greater<int>());
23
return solve(quantity, count, 0);
24
}
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0