Skip to content

Commit 73847e3

Browse files
authored
Create 1283-find-the-smallest-divisor-given-a-threshold.js
1 parent 26530bc commit 73847e3

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} threshold
4+
* @return {number}
5+
*/
6+
const smallestDivisor = function(nums, threshold) {
7+
let l = 1, r = 1e6
8+
while(l < r) {
9+
const mid = l + Math.floor((r - l) / 2)
10+
if(valid(mid)) r = mid
11+
else l = mid + 1
12+
}
13+
return l
14+
15+
function valid(mid) {
16+
let res = 0
17+
for(let e of nums) res += Math.ceil(e / mid)
18+
return res <= threshold
19+
}
20+
};

0 commit comments

Comments
 (0)