其他分享
首页 > 其他分享> > 【二分查找】1283. 使结果不超过阈值的最小除数

【二分查找】1283. 使结果不超过阈值的最小除数

作者:互联网

题目:

 

 

解答:

 

 

 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 };

 

标签:begin,1283,阈值,nums,int,ans,threshold,public,除数
来源: https://www.cnblogs.com/ocpc/p/12830521.html