Minimized Maximum of Products Distributed to Any Store
Time complexity: $O(m\log N)$
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
|
#include <iostream>
#include <vector>
class Solution {
public:
int minimizedMaximum(int n, std::vector<int>& quantities) {
int m = quantities.size();
int left = 1, right = 0;
for (int i : quantities) {
right = std::max(right, i);
}
n -= m;
if (n == 0) {
return right;
}
for (int mid = (left + right) / 2; right >= left;) {
if (check(mid, n, quantities)) {
right = mid - 1;
}
else {
left = mid + 1;
}
mid = (left + right) / 2;
}
return left;
}
private:
bool check(int x, int n, std::vector<int>& quantities) {
for (int i : quantities) {
n -= (i - 1) / x;
if (n < 0) {
return false;
}
}
return true;
}
};
|
