Find the Smallest Divisor Given a Threshold LeetCode solution in Java

1
class Solution {
2
public int smallestDivisor(int[] a, int h) {
3
int l = 1, r = a[0];
4
for (int x : a) if (x > r) r = x;
5

6
while (l < r) {
7
int m = l + (r - l) / 2;
8
if (valid(a, m, h)) r = m;
9
else l = m + 1;
10
}
11

12
return l;
13
}
14

15
private boolean valid(int[] a, int m, int h) {
16
for (int x : a) if ((h -= (x + m - 1) / m) < 0) return false;
17
return true;
18
}
19
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0