Minimized Maximum of Products Distributed to Any Store
Medium
Topics
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
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
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
- Search
xover[1, max(nums)]. A cap above the largest stock buys nothing. - For the midpoint, sum
ceil(v / x)over all types, written in integers as(v + x - 1) // x. - If the total is within
target, the cap is feasible, so movehidown to it; otherwise moveloabove it. - 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 4Complexity
- 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 = 2silently understates it and the search returns a cap that is too small. - Writing the ceiling as
v / x + 1, which overcounts by one wheneverxdividesvexactly. - 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 < hiwithhi = midand a finalreturn lois one consistent form;while lo <= hineedshi = mid - 1and a separately tracked answer. Combininglo <= hiwithhi = midnever 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 along.
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 loimport java.util.*;
class Solution {
public int minimizedMaximum(int[] nums, int target) {
int lo = 1, hi = 0;
for (int x : nums) hi = Math.max(hi, x);
while (lo < hi) {
int mid = (lo + hi) / 2;
if (stores(nums, mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
}
long stores(int[] nums, int x) {
long total = 0;
for (int v : nums) total += (v + x - 1) / x;
return total;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int minimizedMaximum(vector<int>& nums, int target) {
int lo = 1, hi = 0;
for (int x : nums) hi = max(hi, x);
auto stores = [&](int x) {
long long total = 0;
for (int v : nums) total += (v + x - 1) / x;
return total;
};
while (lo < hi) {
int mid = (lo + hi) / 2;
if (stores(mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var minimizedMaximum = function(nums, target) {
let lo = 1, hi = 0;
for (const x of nums) hi = Math.max(hi, x);
const stores = (x) => {
let total = 0;
for (const v of nums) total += Math.floor((v + x - 1) / x);
return total;
};
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (stores(mid) <= target) hi = mid;
else lo = mid + 1;
}
return lo;
};