Premium Only Content

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.
Example 1:
Input: n = 6, quantities = [11,6]
Output: 3
Explanation: One optimal way is:
- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.
Example 2:
Input: n = 7, quantities = [15,10,10]
Output: 5
Explanation: One optimal way is:
- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.
Example 3:
Input: n = 1, quantities = [100000]
Output: 100000
Explanation: The only optimal way is:
- The 100000 products of type 0 are distributed to the only store.
The maximum number of products given to any store is max(100000) = 100000.
Constraints:
m == quantities.length
1 <= m <= n <= 105
1 <= quantities[i] <= 105
class Solution {
public:
bool canDistribute(int x, vector<int>& quantities, int n) {
// Pointer to the first not fully distributed product type
int j = 0;
// Remaining quantity of the jth product type
int remaining = quantities[j];
// Loop through each store
for (int i = 0; i < n; i++) {
// Check if the remaining quantity of the jth product type
// can be fully distributed to the ith store
if (remaining <= x) {
// If yes, move the pointer to the next product type
j++;
// Check if all products have been distributed
if (j == quantities.size()) {
cout<<remaining<<" ";
return true;
} else {
remaining = quantities[j];
}
} else {
// Distribute the maximum possible quantity (x) to the ith store
remaining -= x;
}
}
return false;
}
int minimizedMaximum(int n, vector<int>& quantities) {
// Initialize the boundaries of the binary search
int left = 0;
int right = *max_element(quantities.begin(), quantities.end());
// Perform binary search until the boundaries converge
while (left < right) {
int middle = (left + right) / 2;
if (canDistribute(middle, quantities, n)) {
// Try for a smaller maximum
right = middle;
} else {
// Increase the minimum possible maximum
left = middle + 1;
}
}
return left;
}
};
-
1:04:51
MTNTOUGH Podcast w/ Dustin Diefenderfer
20 hours agoJustin Wren: From MMA to Congo, The Wild Story Behind His Vanishing Act | MTNPOD #138
3.71K1 -
7:47
The Shannon Joy Show
13 hours agoRepublicans need to WAKE UP... feat. Maria Zeee
3.41K5 -
4:28
DropItLikeItsScott
13 hours agoI Tried To Break The STOPBOX Here's What Happened!
3.32K1 -
21:16
Actual Justice Warrior
14 hours agoMedia CALLS OUT Chicago Mayor's LIES
21.4K5 -
31:38
daniellesmithab
3 days agoAlberta Update: Potential Pipeline for Alberta
14K4 -
33:38
Stephen Gardner
15 hours ago🔥Democrats just SCREWED themselves in SHUTDOWN Trap!!
88.7K32 -
1:41:32
Badlands Media
17 hours agoBaseless Conspiracies Ep. 155: Robotheism
36K56 -
1:03:40
TheCrucible
14 hours agoThe Extravaganza! EP: 56 with Geust co-host Rob Noerr (10/20/25)
169K21 -
2:03:29
Inverted World Live
11 hours agoNASA Says Earth Has Two Moons, 3I/Atlas Hiding Behind The Sun | Ep. 126
148K20 -
3:10:08
TimcastIRL
11 hours agoNO KINGS Protester Yells KILL DHS, Liberal Tries KILLING Trump Supporter At His Home | Timcast IRL
266K126