Best run of days
Daily profits can be negative. Find the largest total over any run of
consecutive days (at least one day). One method: walk the days keeping a
running total of the current run. For each day, in this order: add the
day to the running total; if the running total is now the best seen so
far, remember it; then, if the running total is below zero, reset it to
zero, because a negative run can only hurt whatever comes next. Because a
run must have at least one day, start best at the first day's profit,
not at zero, so that an all-negative list still gives the least bad day.
Write best_run(profits) for a non-empty list.
best_run([-2, 1, -3, 4, -1, 2, 1, -5, 4]) -> 6 (4 - 1 + 2 + 1)
best_run([-3, -1, -2]) -> -1
def best_run(profits):
...