Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
6-lines: min, itertools.combinations solution in Clear category for Loading Cargo by Phil15
from itertools import chain, combinations
def checkio(weights: list) -> int:
if len(weights) <= 1: return sum(weights)
N, w = sum(weights), len(weights)//2
# We have to find a sublist L such that sum(L) is as close as possible to N/2.
# With len(L) <= len(weights)//2 == w. We minimize 2 * abs(N/2 - sum(L)).
return min(abs(N - 2 * sum(L))
for L in chain(*(combinations(weights, k+1) for k in range(w))))
Nov. 1, 2018