Skip to contentKEEP THE CRAFTTHE SANCTUARY

Best Sum Ending at Each Index

Record Kadane's running value: the best sum of a run that ends exactly at each index.

The problem

Implement kadane_ending_states(values). For each index i, find the largest sum of a non-empty contiguous run that ends exactly at index i, and return these sums as a list. For example, [-2, 3, -1, 4, -6, 2] gives [-2, 3, 2, 6, 0, 2]: the best run ending at index 3 is [3, -1, 4] with sum 6. A run must contain at least one value, so [-4, -2, -7] gives [-4, -2, -7]. Return [] for an empty list.

Starter code

def kadane_ending_states(values):
    pass

Public examples

  1. values = [-2, 3, -1, 4, -6, 2]gives[-2, 3, 2, 6, 0, 2]
  2. values = [-4, -2, -7]gives[-4, -2, -7]

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

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