Skip to content

Commit f1e4d18

Browse files
authored
Create Minimized Maximum of Products Distributed to Any Store.java
1 parent 5a3795b commit f1e4d18

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public int minimizedMaximum(int n, int[] quantities) {
3+
int left = 0;
4+
int right = 0;
5+
for (int quantity : quantities) {
6+
right = Math.max(right, quantity);
7+
}
8+
while (left < right) {
9+
int mid = (left + right) / 2;
10+
if (isPossible(n, mid, quantities)) {
11+
right = mid;
12+
} else {
13+
left = mid + 1;
14+
}
15+
}
16+
return left;
17+
}
18+
19+
private boolean isPossible(int n, int min, int[] quantities) {
20+
int idx = 0;
21+
int remaining = quantities[idx];
22+
for (int i = 0; i < n; i++) {
23+
if (remaining <= min) {
24+
idx++;
25+
if (idx == quantities.length) {
26+
return true;
27+
} else {
28+
remaining = quantities[idx];
29+
}
30+
} else {
31+
remaining -= min;
32+
}
33+
}
34+
return false;
35+
}
36+
}

0 commit comments

Comments
 (0)