Skip to content

Commit 13ba086

Browse files
committed
solved: 1283
1 parent 16884ff commit 13ba086

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @leet start
2+
3+
/** TLE */
4+
// function smallestDivisor(nums: number[], threshold: number): number {
5+
// for(let i = 1; i <= Math.max(...nums); i++) {
6+
// const divSum = nums.reduce((acc, num) => acc + Math.ceil(num / i), 0);
7+
// if(divSum <= threshold) {
8+
// return i;
9+
// }
10+
// }
11+
// throw new Error('No divisor found');
12+
// };
13+
14+
/*
15+
* as divisor grows, the sum of division of all numbers decreases
16+
* so we can use binary search to find the smallest divisor
17+
*/
18+
function smallestDivisor(nums: number[], threshold: number): number {
19+
let left = 1;
20+
let right = Math.max(...nums);
21+
while (left < right) {
22+
/* calcutate mid */
23+
const mid = Math.floor((left + right) / 2);
24+
const divSum = nums.reduce((acc, num) => acc + Math.ceil(num / mid), 0);
25+
if (divSum > threshold) {
26+
left = mid + 1;
27+
} else {
28+
right = mid;
29+
}
30+
}
31+
return left;
32+
}
33+
// @leet end
34+

0 commit comments

Comments
 (0)