래원

[LeetCode] 2064. Minimized Maximum of Products Distributed to Any Store - Python 본문

알고리즘/LeetCode

[LeetCode] 2064. Minimized Maximum of Products Distributed to Any Store - Python

Laewon Jeong 2024. 11. 14. 16:34

 

문제 설명


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.

 

문제 예제


Example 1

Input: n = 6, quantities = [11,6]
Output: 3
Explanation: One optimal way is:
- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.

Example 2

Input: n = 7, quantities = [15,10,10]
Output: 5
Explanation: One optimal way is:
- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.

Example 3

Input: n = 1, quantities = [100000]
Output: 100000
Explanation: The only optimal way is:
- The 100000 products of type 0 are distributed to the only store.
The maximum number of products given to any store is max(100000) = 100000.

 

제한 사항


  • m == quantities.length
  • 1 <= m <= n <= 105
  • 1 <= quantities\[i\] <= 105

 

✏️ 내 풀이

def can_distribute(k, n, quantities):
    if k == 0:
        return False
    
    x = 0
    for q in quantities:
        x += ceil(q/k)
        print(x)
	
    if x <= n:
    	return True
    else:
    	return False  

class Solution:
    def minimizedMaximum(self, n: int, quantities: List[int]) -> int:
        if n == len(quantities):
            return max(quantities)
            
        left, right = 0, max(quantities)

        while left < right:
            mid = (left+right)//2

            if can_distribute(mid, n, quantities):
                right = mid
            else:
                left = mid + 1
        
        return left

 

이 문제는 각 상점에 배정되는 제품의 최대 수량을 최소화하면 되는 문제였다.

 

즉, 특정 상점이 너무 많은 수량을 받는 것을 방지하고, 상점들 사이에 상품을 최대한 균등하게 나눠서 각 상점의 최대 상품 수량을 반환하면 되는 문제였다.

 

처음에는 단순히 return ceil(sum(quantities) / n)와 같이 코드를 제출했으나, 몇몇 테스트케이스가 틀리는 것을 확인했다.

 

그러다 생각을 해보니 답이 될 수 있는 것은 1 ~ max(quantities) 범위에 있는 값이라는 것을 알게 되었다.

 

어떠한 범위에서 특정 값을 찾는 것은 이진 탐색이 효율적이다.

최근 LeetCode가 Daily 문제를 이진탐색 문제만 내주는 느낌이 있다...

 

따라서, 1 ~ max(quantities) 범위에서 최소로 n개의 상점에 분할할 수 있는 값을 찾으면 된다.

 

이를 위해 can_distribute() 함수를 작성했고, 이진탐색 알고리즘과 결합해서 최소로 분할될 수 있는 값을 찾을 수 있었다.


[LeetCode] 2064. Minimized Maximum of Products Distributed to Any Store