Minimized Maximum of Products Distributed to Any Store

Medium

Topics
ArrayBinary Search

You have n product types. The ith type has nums[i] units in stock. You want to distribute all units across target specialty stores with these rules:

  • Each store carries at most one product type (but as many units of that type as you assign).
  • Some stores may remain empty.

Minimize the maximum number of units assigned to any single store. Return that minimized maximum.

Example 1

11061
Input:  nums = [11,6], target = 10
Output: 2
Explanation: Assign product 0 (11 units) across 6 stores: ceil(11/2)=6 stores each getting at most 2. Assign product 1 (6 units) across 3 stores: ceil(6/2)=3 stores each getting at most 2. Uses 9 stores total (<=10). The max any store gets is 2.

Example 2

150101102
Input:  nums = [15,10,10], target = 7
Output: 5
Explanation: With max=5: product 0 needs ceil(15/5)=3, product 1 needs ceil(10/5)=2, product 2 needs ceil(10/5)=2. Total=7 stores. With max=4: 4+3+3=10>7. So 5 is the answer.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5
  • nums.length <= target <= 10^5
Editorial — intuition, approach, complexity and pitfalls

Intuition

Minimising a maximum under a feasibility constraint is the signature of binary search on the answer. Guess a cap x on units per store and ask whether the distribution fits in target stores.

Under a cap of x, product type i needs ceil(nums[i] / x) stores, because a store carries only one type. Feasibility is sum(ceil(nums[i] / x)) <= target, and it is monotone — raising the cap never needs more stores.

Approach

  1. Search x over [1, max(nums)]. A cap above the largest stock buys nothing.
  2. For the midpoint, sum ceil(v / x) over all types, written in integers as (v + x - 1) // x.
  3. If the total is within target, the cap is feasible, so move hi down to it; otherwise move lo above it.
  4. The loop converges on the smallest feasible cap.

Dry Run

nums = [2,4,6]   target = 4

  lo  hi  mid   stores needed   total   fits in target
  1   6   3     1+2+2           5       no    -> cap too small
  4   6   5     1+1+2           4       yes   -> try smaller
  4   5   4     1+1+2           4       yes   -> try smaller

answer 4

Complexity

  • Time — O(n log(max(nums))): about 17 iterations at the stated bound of 10⁵, each a linear pass.
  • Space — O(1).

Common Pitfalls

  • Using floor division. A stock of 5 under a cap of 2 needs three stores, not two; 5 // 2 = 2 silently understates it and the search returns a cap that is too small.
  • Writing the ceiling as v / x + 1, which overcounts by one whenever x divides v exactly.
  • Setting hi = target. The cap is a number of units, not a number of stores; the two are unrelated quantities that happen to share a bound here.
  • Mixing the two binary-search idioms. while lo < hi with hi = mid and a final return lo is one consistent form; while lo <= hi needs hi = mid - 1 and a separately tracked answer. Combining lo <= hi with hi = mid never terminates.
  • Accumulating the store count in a 32-bit integer. With n = 10⁵ types and a cap of 1 the total reaches 10₁⁰, so the shown Java sums into a long.

Full Solution — all languages

class Solution:
    def minimizedMaximum(self, nums, target):
        def stores(x):
            total = 0
            for v in nums:
                total += (v + x - 1) // x
            return total
        lo, hi = 1, max(nums)
        while lo < hi:
            mid = (lo + hi) // 2
            if stores(mid) <= target:
                hi = mid
            else:
                lo = mid + 1
        return lo
SensAI
Beta
Tap the mic to stop voice mode