distribute
of length k
with all zeros, representing the total cookies each child has received.dfs(i, zero_count)
to distribute the cookie:
n - i < zero_count
, return float('inf')
as the distribution is invalid.i == n
, return the maximum value in distribute
as the unfairness of this distribution.answer
to float('inf')
.j
in distribute
:
cookies[i]
to distribute[j]
.distribute[j]
was 0 before adding, decrement zero_count
.dfs(i + 1, zero_count)
and update answer
with the minimum unfairness.cookies[i]
from distribute[j]
(backtrack).distribute[j]
is 0 after removing, increment zero_count
.dfs(0, k)
as the final answer.