Skip to content

Commit 41402a3

Browse files
authored
Update 2064-minimized-maximum-of-products-distributed-to-any-store.js
1 parent 6073326 commit 41402a3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

2064-minimized-maximum-of-products-distributed-to-any-store.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,28 @@ var minimizedMaximum = function(n, quantities) {
2222
for (let x of quantities) cnt += Math.floor(x / max) + ((x % max) ? 1 : 0);
2323
return cnt;
2424
}
25+
26+
// another
27+
28+
/**
29+
* @param {number} n
30+
* @param {number[]} quantities
31+
* @return {number}
32+
*/
33+
const minimizedMaximum = function(n, quantities) {
34+
let MAX = 0;
35+
for (let x of quantities) MAX = Math.max(x, MAX);
36+
let l = 1, r = MAX;
37+
while (l < r) {
38+
let mid = Math.floor((l + r) / 2);
39+
if (valid(quantities, mid, n)) l = mid + 1;
40+
else r = mid;
41+
}
42+
return l;
43+
};
44+
45+
function valid(quantities, max, n) {
46+
let cnt = 0;
47+
for (let x of quantities) cnt += Math.floor(x / max) + ((x % max) ? 1 : 0);
48+
return cnt > n;
49+
}

0 commit comments

Comments
 (0)