1283. Find the Smallest Divisor Given a Threshold
class Solution {
public:
int calDivisionSum(vector<int>& nums, int divisor){
int result = 0;
for(int& num : nums){
result += (num / divisor) + (num % divisor != 0);
}
return result;
}
int smallestDivisor(vector<int>& nums, int threshold) {
int l = 1;
int r = *max_element(nums.begin(), nums.end());
int m;
int pivot;
while(l <= r){
m = l + (r - l) / 2;
pivot = calDivisionSum(nums, m);
if(pivot == threshold){
r = m - 1;
} else if(pivot > threshold){
l = m + 1;
} else {
r = m - 1;
}
}
return l;
}
};