题目:
解答:
1 class Solution { 2 public: 3 int smallestDivisor(vector<int>& nums, int threshold) 4 { 5 int l = 1, r = *max_element(nums.begin(), nums.end()); 6 int ans = -1; 7 while (l <= r) 8 { 9 int mid = (l + r) / 2; 10 int total = 0; 11 for (int num: nums) 12 { 13 total += (num - 1) / mid + 1; 14 } 15 if (total <= threshold) 16 { 17 ans = mid; 18 r = mid - 1; 19 } 20 else 21 { 22 l = mid + 1; 23 } 24 } 25 return ans; 26 } 27 };