Clarifying Some Questions About Dynamic Programming
This article is a revised version of the previous article Dynamic Programming Q&A. Based on my continued learning and feedback from readers, I have expanded it with more content. I aim to make this a comprehensive Q&A article following the Core Patterns of Dynamic Programming. The main content is below.
This article will answer the following questions for you:
What exactly is "optimal substructure," and how is it related to dynamic programming?
How to determine if a problem is a dynamic programming problem, that is, how to identify overlapping subproblems.
Why is the size of the
dp
array often set ton + 1
instead ofn
?Why are there so many ways to traverse the
dp
array in dynamic programming: sometimes forward, sometimes backward, and sometimes diagonally?