Skip to contentKEEP THE CRAFTTHE SANCTUARY

Minimize Adjacent Merge Cost

Fill a table of the cheapest merge cost for every range, from short ranges to long ones.

The problem

Implement minimum_adjacent_merge_cost(weights). weights is a row of groups. In one move you merge two neighboring groups into a single group whose weight is their sum, and that move costs the same sum. Keep merging until only one group is left. Return the smallest possible total cost. An empty list or a single group costs 0. For example, with [3, 2, 4] you can merge 3 and 2 (cost 5) and then 5 and 4 (cost 9), for a total of 14, which is the cheapest order.

Starter code

def minimum_adjacent_merge_cost(weights):
    pass

Public examples

  1. weights = [3, 2, 4]gives14
  2. weights = [2, 2, 1, 2]gives14

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.