The Indian Engineer

Problem 2064 Minimized Maximum of Products Distributed to Any Store

Posted on 7 mins

Array Binary-Search Greedy

Problem Statement

Link - Problem 2064

Question

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:

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:

Solution

class Solution {
public:
    int minimizedMaximum(int n, vector<int>& quantities) {
        // Initialize the search range for binary search
        int low = 1, high = INT_MIN, res = 0;

        // Find the maximum quantity in the quantities vector to set the upper bound
        for(int x: quantities)
            if(high < x)
                high = x;

        // Perform binary search to find the minimized maximum value of products per store
        while(low <= high) {
            int mid = low + (high - low) / 2;  // Midpoint to test as a possible max value
            int sum = 0;  // Sum represents the total stores needed if max products per store is `mid`

            // Calculate total number of stores required if max products per store is `mid`
            for(int x: quantities)
                sum += (ceil((float)x / float(mid)));  // Calculate number of stores needed for each product

            // Check if this midpoint allows for distribution within the given store limit `n`
            if(sum <= n) {
                res = mid;   // Update the result with current valid mid
                high = mid - 1;  // Try for a smaller maximum by reducing the upper bound
            } else {
                low = mid + 1;  // Increase the lower bound if more stores are needed than available
            }
        }

        return res;
    }
};

Complexity Analysis

| Algorithm     | Time Complexity           | Space Complexity |
| ------------- | ------------------------- | ---------------- |
| Binary Search | O(m log(max(quantities))) | O(1)             |

Explanation

Intial Thoughts

This problem requires distributing products into stores such that the maximum number of products per store is minimized. Given the constraints of m <= n and 1 <= quantities[i] <= 10^5, a brute force approach would not be feasible. A binary search could potentially work, as the answer should be between the minimum and maximum quantity values. We also need to find the maximum quantity value to determine our upper search bound.

Intuitive Analysis

The problem essentially boils down to a division problem, distributing quantities as evenly as possible. Each iteration of the binary search represents a maximum products per store value. For each quantity, the number of required stores is calculated using ceiling division. If the total required stores are less than or equal to n, we know the answer is less than or equal to the current mid value and we continue searching the lower half. If the total required stores exceed n, we increase the lower bound to find a larger maximum products per store. The iterative approach will eventually converge to the optimal result.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

There exists a monotonic nature such that when x is smaller than some number, there will be no way to distribute, and when x is not smaller than that number, there will always be a way to distribute.

If you are given a number k, where the number of products given to any store does not exceed k, could you determine if all products can be distributed?

Implement a function canDistribute(k), which returns true if you can distribute all products such that any store will not be given more than k products, and returns false if you cannot. Use this function to binary search for the smallest possible k.


Similar Questions:

Title URL Difficulty
Koko Eating Bananas https://leetcode.com/problems/koko-eating-bananas Medium
Capacity To Ship Packages Within D Days https://leetcode.com/problems/capacity-to-ship-packages-within-d-days Medium
Maximum Candies Allocated to K Children https://leetcode.com/problems/maximum-candies-allocated-to-k-children Medium
Find the Smallest Divisor Given a Threshold https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold Medium
Magnetic Force Between Two Balls https://leetcode.com/problems/magnetic-force-between-two-balls Medium
Minimum Limit of Balls in a Bag https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag Medium
Minimum Time to Complete Trips https://leetcode.com/problems/minimum-time-to-complete-trips Medium
Maximum Number of Robots Within Budget https://leetcode.com/problems/maximum-number-of-robots-within-budget Hard