Find the Smallest Divisor Given a Threshold LeetCode solution in C++

1
class Solution {
2
public:
3
int smallestDivisor(vector<int> &nums, int threshold) {
4
int l = 1, r = 0;
5
for (int n : nums) r = max(r, n);
6

7
int mid, s = 0;
8
while (l < r) {
9
s = 0;
10
mid = l + (r - l) / 2;
11
for (int n : nums) {
12
s += n / mid + (n % mid != 0);
13
}
14
if (s <= threshold) {
15
r = mid;
16
} else {
17
l = mid + 1;
18
}
19
}
20
return r;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0