Overview
Find the best-paid set of shifts with no two days in a row by building on answers to smaller problems, where greedy picks and plain recursion fail.
- build big answers from small ones
- skip today, or take it plus two days ago
- greedy picks fail
Short Course
Dynamic programming builds the answer from answers to smaller problems and stores them, so each is computed once. Here, the best total up to a day either skips it or takes it plus the best up to two days before. Trap: grabbing the best-paid day first can lose.
ways = [1, 1] # ways to climb 0 and 1 stairs
for n in range(2, 6):
ways.append(ways[n - 1] + ways[n - 2])
ways # [1, 1, 2, 3, 5, 8]
Task
Write best_shifts(pay): the most you can earn working some of the days in
pay, never two days in a row. Example: best_shifts([5, 1, 1, 5]) → 10.
Test Suites
Pending
Next
Output