-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallest_Divisor_Given_A_Threshold.cpp
More file actions
44 lines (43 loc) · 1.03 KB
/
Smallest_Divisor_Given_A_Threshold.cpp
File metadata and controls
44 lines (43 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
class Solution {
public:
bool possible(vector<int> &nums, int x, int threshold)
{
int sum = 0;
for(int i = 0; i < nums.size(); i++)
{
sum += ceil((float)nums[i]/x);
}
if(sum <= threshold) return true;
return false;
}
int maxi(vector<int> &nums)
{
int maxx = nums[0];
for(int i = 1; i < nums.size(); i++)
{
if(maxx < nums[i])
maxx = nums[i];
}
return maxx;
}
int smallestDivisor(vector<int>& nums, int threshold) {
int ans = 1;
int low = 1;
int high = maxi(nums);
while(low <= high)
{
int mid = (low + high) / 2;
if(possible(nums, mid, threshold))
{
ans = mid;
high = mid - 1;
}
else low = mid + 1;
}
return ans;
}
};