Minimized Maximum of Products Distributed to Any Store - Problem

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 i-th 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.

Input & Output

Example 1 — Basic Case
$ Input: n = 6, quantities = [11,6]
Output: 3
💡 Note: With max 3 per store: Product type 0 needs ceil(11/3) = 4 stores, product type 1 needs ceil(6/3) = 2 stores. Total: 4 + 2 = 6 stores ≤ 6.
Example 2 — Single Product Type
$ Input: n = 7, quantities = [15]
Output: 5
💡 Note: Only one product type with 15 units. With max 5 per store: ceil(15/5) = 3 stores ≤ 7. With max 4: ceil(15/4) = 4 stores ≤ 7. With max 3: ceil(15/3) = 5 stores ≤ 7. With max 2: ceil(15/2) = 8 stores > 7.
Example 3 — Multiple Small Quantities
$ Input: n = 1, quantities = [100000]
Output: 100000
💡 Note: Only 1 store available, so it must take all 100000 products of the single type.

Constraints

  • m == quantities.length
  • 1 ≤ m ≤ n ≤ 105
  • 1 ≤ quantities[i] ≤ 105

Visualization

Tap to expand
Minimize Maximum Products DistributionInputn = 6 stores11 unitsProduct Type 06 unitsProduct Type 1Optimal Distribution (max=3)3332Store 1Store 2Store 3Store 433Store 5Store 6Result3Minimum MaximumTotal stores used: ceil(11/3) + ceil(6/3) = 4 + 2 = 6 ≤ 6 ✓Each store gets at most 3 products of one type
Understanding the Visualization
1
Input
n=6 stores, quantities=[11,6] product types
2
Distribution
Find minimum possible maximum per store
3
Output
Minimum maximum = 3
Key Takeaway
🎯 Key Insight: We can binary search on the answer space because if maximum x works, then any larger maximum also works
Asked in
Google 15 Amazon 12 Microsoft 8
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen