Skip to content

Commit 2927963

Browse files
authored
Update 2226-maximum-candies-allocated-to-k-children.js
1 parent fed2395 commit 2927963

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

2226-maximum-candies-allocated-to-k-children.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,23 @@ const maximumCandies = function(candies, k) {
2020
}
2121
return min;
2222
};
23+
24+
// another
25+
26+
/**
27+
* @param {number[]} candies
28+
* @param {number} k
29+
* @return {number}
30+
*/
31+
const maximumCandies = function(candies, k) {
32+
let max = candies.reduce((ac, e) => ac + e, 0)
33+
let min = 0
34+
while(min < max) {
35+
const mid = max - Math.floor((max - min) /2)
36+
let num = 0
37+
for(let e of candies) num += ~~(e / mid)
38+
if(num < k) max = mid - 1
39+
else min = mid
40+
}
41+
return min
42+
};

0 commit comments

Comments
 (0)