Problem 598654 · easy · Phase 05 Advanced Algorithms & Graphs

Orchard Harvest

dynamic programming · 1-D dp · take or skip

Fruit trees stand in a row and tree i carries yields[i] kilograms. Picking a tree damages both neighbours, so you may never pick two adjacent trees. Return the largest total weight you can harvest.

Examples

Input:  yields = [4, 9, 2, 7, 3]
Output: 16
Explanation: pick trees 1 and 3 (9 + 7).

Input:  yields = [5]
Output: 5

Constraints

  • 0 <= len(yields) <= 10**5
  • 0 <= yields[i] <= 10**4
  • Target complexity: O(n) time; enumerating subsets is exponential.

Goals

  • Model a take-or-skip decision as a two-way recurrence
  • Replace a full dp table with two rolling values
Starting Python…