Minimized Maximum of Products Distributed to Any Store LeetCode solution in Java

1
class Solution {
2
public int minimizedMaximum(int n, int[] quantities) {
3

4
int lo = 1;
5
int hi = (int) 1e5;
6

7
int ans = -1;
8

9
while (lo <= hi) {
10

11
int mid = (lo + hi) / 2;
12

13
if (isItPossible(mid, quantities, n)) {
14
ans = mid;
15
hi = mid - 1;
16
} else {
17
lo = mid + 1;
18
}
19
}
20

21
return ans;
22
}
23

24
private boolean isItPossible(int x, int[] quantities, int n) {
25

26
// isItPossible to distribute <= x products to each of the n shops
27
for (int i = 0; i < quantities.length; i++) {
28

29
int products = quantities[i];
30

31
n -= Math.ceil(products / (x * 1.0));
32

33
if (n < 0) // means it requires more than n shops to distribute all products
34
return false;
35
}
36

37
return true; // distributed all products to exactly n shops
38
}
39
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0