Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Can Balance by bravebug
from itertools import permutations
from typing import Iterable
def can_balance(weights: Iterable) -> int:
if len(weights) > 1:
length = len(weights)
perms = [item for item in permutations(weights, length)]
diff = {abs(sum(x[:y]) - sum(x[y:])) for x in perms for y in range(1, length)}
if 0 in diff:
return -1
else:
return min(diff)
else:
return 0
Oct. 1, 2021
Comments: