Practice Discrete Math

Algorithms / Dp State Design

Least You Need to Know: Dynamic Programming State Design

Dynamic programming starts by choosing a state that remembers exactly the information a smaller subproblem needs. Good state design is usually the difference between a clean recurrence and a confused one.

جو کم از کم جاننا ضروری ہے

اہم علامتیں

dp[i] answer for subproblem ending at or starting from index i
dp[r][c] answer for the subproblem at grid cell (r,c)
state minimal information needed to describe a subproblem

مختصر حل شدہ مثال

  • In the climb-stairs problem, a natural state is `dp[i] = number of ways to reach step i`.
  • The next move comes from step `i-1` or `i-2`.
  • That gives the recurrence `dp[i] = dp[i-1] + dp[i-2]`.
  • The good state uses just the step index because no other history matters.

عام غلطیاں

اس قسم کے سوال کو کیسے پہچانیں

Next recommended lesson

Continue through this topic with Least You Need to Know: DP as a Subproblem Graph.

Least You Need to Know: DP as a Subproblem Graph

Related lessons

Keep going with nearby lessons in the same topic.

More ways to explore

مشق شروع کریں