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;
}
};
-
LIVE
Adam Does Movies
2 days agoTalking Movies + Ask Me Anything - LIVE
168 watching -
LIVE
I_Came_With_Fire_Podcast
11 hours agoNASA Blocks China, TPUSA BOOSTED, Chinese Spamoflauge, & Factional Division
125 watching -
33:40
Jamie Kennedy
3 hours agoEp 222 Processing the Loss of Charlie Kirk | HTBITY with Jamie Kennedy
4.12K8 -
LIVE
Badlands Media
18 hours agoAltered State S3 Ep. 46
2,014 watching -
9:18
ARFCOM News
7 hours ago $0.29 earnedNSSF "Celebrates" ATF Partnership | Glocks BANNED | Redundant Spooky Boi Ban
3.57K5 -
LIVE
LFA TV
16 hours agoLFA TV ALL DAY STREAM - WEDNESDAY 9/17/25
655 watching -
1:00:00
BEK TV
1 day agoAPRIL LUND: FAITH, FOCUS, AND THE ROAD TO THE 2028 OLYMPIC MARATHON
3.52K -
37:15
Stephen Gardner
2 hours ago🔥Trump ERUPTS After Obama’s Charlie Kirk Comments!
12.5K48 -
LIVE
Total Horse Channel
14 hours ago2025 WDAA Western Dressage World Championship Show | Day Two | Arena One
115 watching -
1:14:40
Glenn Greenwald
4 hours agoThe Right Wages Its Own Cancel Culture War: Lee Fang, Thomas Chatterton Williams, and Leighton Woodhouse on the State of Civil Discourse and More | SYSTEM UPDATE #517
154K61