-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
36 lines (34 loc) · 891 Bytes
/
Solution.java
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 {
private int[] cookies;
private int[] cnt;
private int k;
private int n;
private int ans = 1 << 30;
public int distributeCookies(int[] cookies, int k) {
n = cookies.length;
cnt = new int[k];
Arrays.sort(cookies);
this.cookies = cookies;
this.k = k;
dfs(n - 1);
return ans;
}
private void dfs(int i) {
if (i < 0) {
// ans = Arrays.stream(cnt).max().getAsInt();
ans = 0;
for (int v : cnt) {
ans = Math.max(ans, v);
}
return;
}
for (int j = 0; j < k; ++j) {
if (cnt[j] + cookies[i] >= ans || (j > 0 && cnt[j] == cnt[j - 1])) {
continue;
}
cnt[j] += cookies[i];
dfs(i - 1);
cnt[j] -= cookies[i];
}
}
}