-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFindTheSmallestDivisorGivenAThreshold.java
51 lines (44 loc) · 1.36 KB
/
FindTheSmallestDivisorGivenAThreshold.java
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
45
46
47
48
49
50
51
/*https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/*/
class Solution {
int[] nums;
int threshold;
public int smallestDivisor(int[] nums, int threshold) {
this.nums= nums;
this.threshold = threshold;
int n = nums.length, low = 1, high = 0, mid, result = -1;
for (int num : nums)
high = Math.max(high,num);
while (low <= high)
{
mid = low+(high-low)/2;
if (isFeasible(mid))
{
result = mid;
high = mid-1;
}
else low = mid+1;
}
return result;
}
private boolean isFeasible(int target)
{
double sum = 0;
for (int num : nums)
sum += Math.ceil((double)num/(double)target);
return (int)sum <= threshold;
}
}
class Solution {
public int smallestDivisor(int[] nums, int threshold) {
int left = 1, right = Integer.MIN_VALUE;
for(int num : nums) right = Math.max(right, num);
while(left <= right) {
int pivot = left + (right - left) / 2;
int count = 0;
for(int num : nums) count += (num - 1) / pivot + 1;
if(count > threshold) left = pivot + 1;
else right = pivot - 1;
}
return left;
}
}