We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2e89930 commit b1884a8Copy full SHA for b1884a8
2305-fair-distribution-of-cookies.js
@@ -0,0 +1,32 @@
1
+/**
2
+ * @param {number[]} cookies
3
+ * @param {number} k
4
+ * @return {number}
5
+ */
6
+const distributeCookies = function(cookies, k) {
7
+ const n = cookies.length
8
+ let res = Infinity
9
+ const arr = Array(n).fill(0)
10
+
11
+ bt(0)
12
+ return res
13
14
+ function bt(idx) {
15
+ if(idx === n) {
16
+ res = Math.min(res, Math.max(...arr))
17
+ return
18
+ }
19
+ const cur = cookies[idx]
20
+ // const visited = new Set()
21
+ for(let i = 0; i < k; i++) {
22
+ const e = arr[i]
23
+ // if(visited.has(i)) continue
24
+ if(cur + e >= res) continue
25
26
+ arr[i] += cur
27
+ bt(idx + 1)
28
+ arr[i] -= cur
29
+ if(arr[i] === 0) break
30
31
32
+};
0 commit comments