You are given an integer n indicating there are
n specialty retail stores. There are m
product types 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 product to the retail
stores following these rules:
- A store can only be given at most one product type but can 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 of products that are given to any store.
Return the minimum possible x.
Example:
1 | Input: n = 6, quantities = [11, 6]; |
1 | Input: n = 7, quantities = [15, 10, 10]; |
1 | Input: n = 1, quantities = [10000]; |
Constraints:
- 1 ≤ m ≤ n ≤ 105
- 1 ≤ quantitiesi ≤ 105
Solution
1 | class Solution { |
Explanation: The key idea of this solution is
binary search, the variable mid
here is
used to indicate the possible maximum quantity that can be distributed
to a single store. Variables low
and high
hold
the lower bound and the upper bound of the quantity for distribution.
Then use binary search to update the variable mid
to find
the possible minimized maximum quantity. The way to update
mid
is to check whether this value can be distributed
within n stores. Here using
variable needed
to hold the number of store that needs for
distributing under mid
. So traverse the
quantities
to compute the needed number of stores to
distribute q
products under mid
is the upper
bound. For example, if q=7
then mid=3
, since 7
products need 3 stores. If needed
is less than n, which means the value of
mid
is greater, and there might a smaller solution, thus
update high
with mid
. Otherwise,
mid
is to small, thus update low
with
mid + 1
. When low
and high
converge, which means the minimized value mid
is found,
then return low
.