2305. Fair Distribution of Cookies
https://leetcode.com/problems/fair-distribution-of-cookies
Python
from math import inf
from functools import cache
class Solution:
def distributeCookies(self, cookies: List[int], k: int) -> int:
n = len(cookies)
@cache
def dfs(mask, remain):
if mask == 0:
return 0
if remain == 0:
return inf
result = inf
origin = mask
while mask:
mask = origin & (mask-1)
total = sum(cookies[i] for i in range(n) if (origin^mask) & 1<<i)
result = min(result, max(total, dfs(mask, remain-1)))
return result
return dfs((1<<n)-1, k)