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:
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.length1 <= m <= n <= 1051 <= quantities[i] <= 105Problem Overview: You are given n stores and an array quantities where each value represents the number of products of a specific type. Products of the same type must be distributed to stores, and a store can only receive one product type. The goal is to minimize the maximum number of products assigned to any store.
Approach 1: Binary Search over the Maximum Products (Time: O(m log M), Space: O(1))
This problem becomes easier if you think in terms of the answer rather than the distribution. Instead of trying to assign products directly, you search for the smallest possible value x such that no store receives more than x products. This value must lie between 1 and max(quantities). Use binary search over this range.
For a candidate value x, check if the distribution is feasible. For each quantity q, compute how many stores are required if each store can hold at most x items: ceil(q / x). Sum these values across all product types. If the total required stores is ≤ n, the candidate maximum is feasible, so try a smaller value. Otherwise increase the bound. This feasibility check is a simple greedy count and runs in linear time over the array.
The key insight: the feasibility condition is monotonic. If a maximum value x works, any larger value will also work. That monotonic property makes binary search the natural tool here. The algorithm scans the array each iteration, giving total complexity O(m log M), where m is the number of product types and M is the maximum quantity.
Approach 2: Greedy Check with Priority Allocation (Time: O(m log n), Space: O(m))
Another perspective is to repeatedly split the largest product loads across stores using a greedy strategy. Start by placing each product type into a max heap with its quantity. Each heap entry tracks the current maximum load per store for that product type based on how many stores it has been split across.
Iteratively assign additional stores to the product type with the largest current load. Each split reduces the maximum load for that product type because the quantity is divided among more stores. Use a priority queue to always process the largest current load first. This method resembles greedy load balancing and relies on a greedy decision at every step.
While intuitive, this approach is less common in interviews because it requires careful heap bookkeeping and slightly higher overhead. However, it still works efficiently and illustrates the idea of progressively balancing the distribution.
Recommended for interviews: Binary search on the answer with a greedy feasibility check. Interviewers expect you to recognize the monotonic constraint and apply binary search to minimize the maximum load. Mentioning a direct greedy distribution first shows problem exploration, but implementing the binary search solution demonstrates stronger algorithmic judgment.
This approach uses binary search to find the minimal possible value of the maximum number of products any store can receive. For each candidate value of maximum products, validate whether the distribution is possible using this value. If distribution is possible, try to lower the maximum; otherwise, increment it.
In this solution, we initialize the binary search range with left being 1 and right as the maximum quantity in the list. For each midpoint, we check if it's possible to distribute all products under this maximum threshold (using ceiling division).
Time Complexity: O(m log(max(quantities))), where m is the number of product types.
Space Complexity: O(1), as we only use a fixed amount of extra space.
This approach tries to distribute products product-by-product to the least filled store using a priority queue. The queue handles product allocation dynamically, prioritizing lesser filled stores to balance distribution.
The solution maintains a heap for retail stores initially set to size n with negative ones. For each quantity, the largest product is distributed to the least loaded store/s, maintaining a balanced load across stores.
Time Complexity: O(m log n + n), where m is the number of products and n the number of stores.
Space Complexity: O(n) for maintaining the heap of stores.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Binary Search over the Maximum Products | Time Complexity: O(m log(max(quantities))), where m is the number of product types. |
| Greedy Check with Priority Allocation | Time Complexity: O(m log n + n), where m is the number of products and n the number of stores. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Binary Search over Maximum Products | O(m log M) | O(1) | Best general solution when the answer space is monotonic and you can check feasibility quickly |
| Greedy Priority Allocation (Heap) | O(m log n) | O(m) | Useful when modeling the problem as load balancing and repeatedly splitting the largest distribution |
Minimized Maximum of Products Distributed to Any Store - Leetcode 2064 - Python • NeetCodeIO • 10,415 views views
Watch 9 more video solutions →Practice Minimized Maximum of Products Distributed to Any Store with our built-in code editor and test cases.
Practice on FleetCode