Python 3D Academyবাংলা

Dynamic Programming 1D in Python

Chapter 57: Dynamic Programming 1D

6 short lessons, with 3D animations, quizzes and coding tasks.

The lessons

  1. Overlapping subproblems: climbing stairs, from recursion to memo

    Dynamic programming (DP) solves a problem from the answers to smaller versions of it, and works out each smaller answer only once, because the same small questions come up again and again.

  2. Filling a table from the bottom up

    Tabulation (bottom-up DP) fills a table of every state in an order where each cell's smaller states are already filled, so there is no recursion and no memo lookup, just a loop.

  3. House robber and coin change

    When each step is a choice, let the recurrence try every option for the last decision and keep the best; the table then holds the best answer for every smaller street.

  4. Designing a DP: the state, the choice, the base case

    Most of the work in DP is choosing what one cell means; say it in one sentence ("dp[i] is the least it costs to stand on step i"), and the recurrence, the base cases and the answer cell follow from that sentence.

  5. The best run: Kadane's algorithm and the best day to sell

    The best run that ends at today is either today alone or yesterday's best run plus today, cur = max(x, cur + x), and the answer is the best cur seen on any day.

  6. Splitting strings: word break and decode ways

    Let the state be a prefix, the first i characters: it can be split (or read) if some last piece is valid and the prefix before that piece could be split (or read) too, so ok[i] and ways[i] are built from smaller prefixes.