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:
A store can only be given at most one product type but can be 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 number of products that are given to any store.
Return the minimum possible x.
n = 7, quantities = [15,10,10]
class Solution:
def minimizedMaximum(self, n: int, quantities: List[int]) -> int:
least = 1
maxima = max(quantities)
def check(maxima: intern):
count = 0
for quantity in quantities:
count += math.ceil(quantity / maxima)
return count <= n
while least < maxima:
mid = (least + maxima) // 2
chk = check(mid)
if chk:
maxima = mid
else:
least = mid + 1
return least
여기서 답의 범위는 1개 ~ 한가지 종류의 물품의 최대 갯수가 될 것이다.
또한 우리는 범위 내 특정 값을 통해 하나를 알아볼수 있는데
이 두가지 경우를 통해 값이 될수 있는 값의 최소 ~ 최대 값에서 이진탐색으로 값을 찾아 나가면 된다.
이진탐색은 O(logN) 이고 체크는 O(N) 이기에 총 O(NlogN) 시간복잡도가 되어 충분히 해결이 가능해진다.
https://velog.io/@ilov1112/Leetcode-875.-Koko-Eating-Bananas-with-Python