Skip to content

Commit da9d268

Browse files
committed
Add solution #1283
1 parent fc562a4 commit da9d268

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,396 LeetCode solutions in JavaScript
1+
# 1,397 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -975,6 +975,7 @@
975975
1277|[Count Square Submatrices with All Ones](./solutions/1277-count-square-submatrices-with-all-ones.js)|Medium|
976976
1278|[Palindrome Partitioning III](./solutions/1278-palindrome-partitioning-iii.js)|Hard|
977977
1282|[Group the People Given the Group Size They Belong To](./solutions/1282-group-the-people-given-the-group-size-they-belong-to.js)|Medium|
978+
1283|[Find the Smallest Divisor Given a Threshold](./solutions/1283-find-the-smallest-divisor-given-a-threshold.js)|Medium|
978979
1284|[Minimum Number of Flips to Convert Binary Matrix to Zero Matrix](./solutions/1284-minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix.js)|Hard|
979980
1286|[Iterator for Combination](./solutions/1286-iterator-for-combination.js)|Medium|
980981
1287|[Element Appearing More Than 25% In Sorted Array](./solutions/1287-element-appearing-more-than-25-in-sorted-array.js)|Easy|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 1283. Find the Smallest Divisor Given a Threshold
3+
* https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of integers nums and an integer threshold, we will choose a positive integer
7+
* divisor, divide all the array by it, and sum the division's result. Find the smallest divisor
8+
* such that the result mentioned above is less than or equal to threshold.
9+
*
10+
* Each result of the division is rounded to the nearest integer greater than or equal to that
11+
* element. (For example: 7/3 = 3 and 10/2 = 5).
12+
*
13+
* The test cases are generated so that there will be an answer.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @param {number} threshold
19+
* @return {number}
20+
*/
21+
var smallestDivisor = function(nums, threshold) {
22+
let left = 1;
23+
let right = Math.max(...nums);
24+
25+
while (left < right) {
26+
const mid = Math.floor((left + right) / 2);
27+
const sum = nums.reduce((acc, num) => acc + Math.ceil(num / mid), 0);
28+
29+
if (sum <= threshold) {
30+
right = mid;
31+
} else {
32+
left = mid + 1;
33+
}
34+
}
35+
36+
return left;
37+
};

0 commit comments

Comments
 (0)