-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1283.java
More file actions
33 lines (31 loc) · 954 Bytes
/
_1283.java
File metadata and controls
33 lines (31 loc) · 954 Bytes
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
package com.github.aditya;
public class _1283 {
// 7 ms, faster than 97.81% - Binary Search
class Solution {
public int smallestDivisor(int[] nums, int threshold) {
int left = 1, right = 0;
for (int num : nums) {
if (num > right)
right = num;
}
int result = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (isValid(nums, threshold, mid)) {
result = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return result;
}
public boolean isValid(int[] arr, int threshold, int divisor) {
int sum = 0;
for (int num : arr) {
sum += (num - 1) / divisor + 1;
}
return sum <= threshold;
}
}
}