|
| 1 | +""" |
| 2 | +author: Zhengjian Kang |
| 3 | +date: 11/15/2021 |
| 4 | +
|
| 5 | +残酷群每日一题: 11/23/2021 |
| 6 | +
|
| 7 | +https://leetcode.com/problems/minimized-maximum-of-products-distributed-to-any-store/ |
| 8 | +
|
| 9 | +2064. Minimized Maximum of Products Distributed to Any Store |
| 10 | +
|
| 11 | +note: binary search by value |
| 12 | +
|
| 13 | +You are given an integer n indicating there are n specialty retail stores. There are m product types of varying amounts, |
| 14 | +which are given as a 0-indexed integer array quantities, where quantities[i] represents the number of products of the ith product type. |
| 15 | +
|
| 16 | +You need to distribute all products to the retail stores following these rules: |
| 17 | +
|
| 18 | +A store can only be given at most one product type but can be given any amount of it. |
| 19 | +After distribution, each store will have been given some number of products (possibly 0). |
| 20 | +Let x represent the maximum number of products given to any store. You want x to be as small as possible, |
| 21 | +i.e., you want to minimize the maximum number of products that are given to any store. |
| 22 | +Return the minimum possible x. |
| 23 | +
|
| 24 | +Example 1: |
| 25 | +Input: n = 6, quantities = [11,6] |
| 26 | +Output: 3 |
| 27 | +Explanation: One optimal way is: |
| 28 | +- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3 |
| 29 | +- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3 |
| 30 | +The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3. |
| 31 | +
|
| 32 | +Example 2: |
| 33 | +Input: n = 7, quantities = [15,10,10] |
| 34 | +Output: 5 |
| 35 | +Explanation: One optimal way is: |
| 36 | +- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5 |
| 37 | +- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5 |
| 38 | +- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5 |
| 39 | +The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5. |
| 40 | +
|
| 41 | +Example 3: |
| 42 | +Input: n = 1, quantities = [100000] |
| 43 | +Output: 100000 |
| 44 | +Explanation: The only optimal way is: |
| 45 | +- The 100000 products of type 0 are distributed to the only store. |
| 46 | +The maximum number of products given to any store is max(100000) = 100000. |
| 47 | + |
| 48 | +
|
| 49 | +Constraints: |
| 50 | +m == quantities.length |
| 51 | +1 <= m <= n <= 10^5 |
| 52 | +1 <= quantities[i] <= 10^5 |
| 53 | +""" |
| 54 | + |
| 55 | +class Solution: |
| 56 | + def minimizedMaximum(self, n: int, quantities: List[int]) -> int: |
| 57 | + left, right = 1, max(quantities) |
| 58 | + |
| 59 | + def is_larger_than_n(val): |
| 60 | + count = 0 |
| 61 | + for x in quantities: |
| 62 | + count += (x+val-1) // val |
| 63 | + return count > n |
| 64 | + |
| 65 | + while left < right: |
| 66 | + mid = left + (right - left) // 2 |
| 67 | + if is_larger_than_n(mid): |
| 68 | + left = mid + 1 |
| 69 | + else: |
| 70 | + right = mid |
| 71 | + |
| 72 | + return left |
| 73 | + |
0 commit comments