-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
48 lines (41 loc) · 1.26 KB
/
main.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public:
int distributeCookies(vector<int>& cookies, int k) {
vector<int> got(k, 0);
int result = numeric_limits<int>::max();
function<void(int)> dfs = [&](int i) -> void {
if (i == cookies.size()) {
result = min(result, *max_element(got.begin(), got.end()));
return;
}
for (int j = 0; j < k; ++j) {
if (got[j] + cookies[i] >= result) continue;
got[j] += cookies[i];
dfs(i + 1);
got[j] -= cookies[i];
}
};
dfs(0);
return result;
}
};
class Solution {
public:
int distributeCookies(vector<int>& cookies, int k) {
int result = accumulate(cookies.begin(), cookies.end(), 0);
vector<int> got(k, 0);
function<void(int)> dfs = [&](int i) -> void {
if (i == cookies.size()) {
result = min(result, *max_element(got.begin(), got.end()));
return;
}
for (int j = 0; j < k; ++j) {
got[j] += cookies[i];
if (got[j] < result) dfs(i + 1);
got[j] -= cookies[i];
}
};
dfs(0);
return result;
}
};