Problem 596373 · medium · Level 05 Advanced Algorithms & Graphs

Two Trucks, One Load

dynamic programming · subset sum · knapsack

A removal company must split crates between two trucks; every crate goes on exactly one truck. The crate weights are crates. Return the smallest possible difference between the two trucks' total loads.

Examples

Input:  crates = [8, 3, 5, 4]
Output: 2
Explanation: 8 + 3 = 11 on one truck, 5 + 4 = 9 on the other.

Input:  crates = [7]
Output: 7

Constraints

  • 0 <= len(crates) <= 100
  • 1 <= crates[i] <= 100
  • There are up to 2**100 splits, so do not enumerate them; the total weight is at most 10**4.

Goals

  • Find every total weight one truck could carry
  • Turn the set of reachable totals into the smallest imbalance
Starting Python…