Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2305. Fair Distribution of Cookies #10

Open
box-lin opened this issue Jul 1, 2023 · 0 comments
Open

2305. Fair Distribution of Cookies #10

box-lin opened this issue Jul 1, 2023 · 0 comments

Comments

@box-lin
Copy link
Owner

box-lin commented Jul 1, 2023

image

Time: O(k^n), each level of recusive stack at most of k.
Space: O(k+n), recursive stack at most depth of n.

class Solution:
    def distributeCookies(self, cookies: List[int], k: int) -> int:
        def dfs(i):
            nonlocal res
            if i >= len(cookies):
                curmax = max(acc)
                res = min(res, curmax) 
                return
            # cut the recursion
            if max(acc) >= res:
                return
            # for a bag of cookie we have k choices (children) to distribute.
            for j in range(k):
                acc[j] += cookies[i]
                dfs(i+1)
                acc[j] -= cookies[i]
        res = math.inf
        acc = [0] * k
        dfs(0)
        return res
@box-lin box-lin added this to the Algorithm milestone Jul 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant