-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path2305. Fair Distribution of Cookies
36 lines (32 loc) · 1.4 KB
/
2305. Fair Distribution of Cookies
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
class Solution {
public:
int k; // Number of jars
int res = INT_MAX; // Result variable to store the minimum maximum jar value
vector<int> d; // Vector to store the current jar values
void dfs(vector<int>& real, int index, int f) {
if (index == real.size()) { // Base case: If all cookies have been distributed
int iron_man = 0;
for (int i = 0; i < k; i++) {
iron_man = max(iron_man, d[i]); // Find the maximum value among all jars
}
res = min(res, iron_man); // Update the result with the minimum maximum jar value
return;
}
for (int i = 0; i < f; i++) {
d[i] += real[index]; // Distribute the cookie to jar 'i'
dfs(real, index + 1, f); // Recurse to distribute the next cookie
d[i] -= real[index]; // Backtrack: Remove the cookie from jar 'i'
}
if (f < k) {
d[f] += real[index]; // Distribute the cookie to a new jar 'f'
dfs(real, index + 1, f + 1); // Recurse to distribute the next cookie
d[f] -= real[index]; // Backtrack: Remove the cookie from jar 'f'
}
}
int distributeCookies(vector<int>& real, int K) {
k = K;
d.resize(K); // Resize the vector 'd' to 'K' jars
dfs(real, 0, 0);
return res;
}
};