[LeetCode] Minimized Maximum of Products Distributed to Any Store

2064. Minimized Maximum of Products Distributed to Any Store

You are given an integer n indicating there are n specialty retail stores. There are m product types of varying amounts, which are given as a 0-indexed integer array quantities, where quantities[i] represents the number of products of the ith product type.

You need to distribute all products to the retail stores following these rules:

  • A store can only be given at most one product type but can be given any amount of it.
  • After distribution, each store will have been given some number of products (possibly 0). Let x represent the maximum number of products given to any store. You want x to be as small as possible, i.e., you want to minimize the maximum number of products that are given to any store.

Return the minimum possible x.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
int helper(vector<int>& A, int k) {
int res = 0;
for(auto& a : A) {
res += (a + k - 1) / k;
}
return res;
}
public:
int minimizedMaximum(int n, vector<int>& A) {
int l = 1, r = *max_element(begin(A), end(A));
int res = r;
while(l <= r) {
int m = l + (r - l) / 2;
int req = helper(A, m);
if(req <= n) {
res = min(m,res);
r = m - 1;
} else l = m + 1;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/09/PS/LeetCode/minimized-maximum-of-products-distributed-to-any-store/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.