Harvest With a Rest Gap
A row of fruit trees is described by yields, where yields[i] is how much fruit tree i gives when you pick it. Picking tires you out: after picking tree i you must leave the next gap trees alone, so the next tree you pick must have an index of at least i + gap + 1. You may pick any number of trees, including none, which gives a total of 0. Return the largest total yield you can collect. With gap = 0 you can pick every tree, and with gap = 1 no two neighbors can both be picked.
Example 1
Trees 1 and 3 give 10 + 7 = 17, the best total with no two neighbors picked.
Example 2
After tree 0 the next pick must be tree 3 or later, so trees 0 and 4 give 6 + 6 = 12.
Constraints
0 <= len(yields) <= 500 <= yields[i] <= 1000 <= gap <= 5
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.