-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributeRepeatingIntegers.cpp
More file actions
36 lines (34 loc) · 1.13 KB
/
distributeRepeatingIntegers.cpp
File metadata and controls
36 lines (34 loc) · 1.13 KB
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
27
28
29
30
31
32
33
34
35
36
// Source: https://leetcode.com/problems/distribute-repeating-integers/
// Author: Miao Zhang
// Date: 2021-05-25
class Solution {
public:
bool canDistribute(vector<int>& nums, vector<int>& quantity) {
unordered_map<int, int> freq;
for (int num: nums) freq[num]++;
vector<int> cnt;
for (auto& [k, f]: freq)
cnt.push_back(f);
int n = cnt.size();
int m = quantity.size();
vector<int> sums(1 << m);
for (int mask = 0; mask < 1 << m; mask++) {
for (int j = 0; j < m; j++) {
if (mask & ( 1 << j)) {
sums[mask] += quantity[j];
}
}
}
vector<vector<int>> dp(1 << m, vector<int>(n + 1));
dp[0][0] = 1;
for (int mask = 0; mask < 1 << m; ++mask) {
for (int j = 0; j < n; j++) {
dp[mask][j + 1] |= dp[mask][j];
for (int cur = mask; cur; cur = (cur - 1) & mask)
if (sums[cur] <= cnt[j] && dp[mask ^ cur][j])
dp[mask][j + 1] = 1;
}
}
return dp[(1 << m) - 1][n];
}
};