2305. Fair Distribution of Cookies

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 2305

class Solution:
    def distributeCookies(self, cookies: List[int], k: int) -> int:
        def dfs(i):
            if i >= len(cookies):
                nonlocal res
                res = max(cnt)
                return
            for j in range(k):
                if cnt[j] + cookies[i] >= res or (j and cnt[j] == cnt[j - 1]):
                    continue
                cnt[j] += cookies[i]
                dfs(i + 1)
                cnt[j] -= cookies[i]

        res = inf
        cnt = [0] * k
        cookies.sort(reverse=True)
        dfs(0)
        return res