Loading topic
dp[i][j] is ...". If that sentence is vague, the recurrence, base cases, and fill order built on it will be wrong. This chapter covers how to write that sentence precisely.2 min readgrid[i][j], and already know what dp[i][j] means for a problem, as covered in the previous chapter. Here you will learn how to turn the moves a problem allows into a recurrence. A recurrence is a formula that computes one 2 min readgrid[i][j], and follow the recurrence idea from the previous chapter: each cell's value comes from its neighbors. Here the question is what happens at cells that are missing some of those neighbors.2 min readdp[i][j] = dp[i-1][j] + dp[i][j-1]. The earlier chapters covered how to define the state and choose base cases. This one covers the order in which you compute the cells.2 min readdp[i][j] = dp[i-1][j] + dp[i][j-1]. It also assumes you can read nested Python loops over a 2D list.2 min readways(i, j) = ways(i - 1, j) + ways(i, j - 1), and you know Big-O notation. Every grid DP can be written in two styles. Both compute the same states with the same recurrence, but they differ in who decides the 2 min readdp[i][j] holds the cheapest cost to reach cell (i, j) moving only right or down. It also assumes you are comfortable with 2D indexing and nested loops.2 min read'1' and '0' characters, find 3 min readfunctools.lru_cache, and the earlier chapters on fill order and top-down memoization.2 min read