1. DP tricks
1.1. Add a dummy / hidden state at dp[0]
In many DP problems, you face annoying edge conditions like: dp[i-1] is undefined for i = 0 where initial transitions require special handling
Take the following state transition functions as an example: dp[i] = dp[i-1] + nums[i] where 0 <= i <= n-1
We need to add dp[0] as a special value/state to generalize the function
One trick is changing the range of DP table indexing dp[i] = dp[i-1] + nums[i-1] where 1 <= i <= n dp[0] = sthspecial
Note that we shift the range of i from [0, n-1] to [1, n], the nums table indexing needs to change from i to i-1
dp[0] = sth_special for (int i = 1; i <= n; ++i) dp[i] = dp[i-1] + nums[i-1]
Another way to change the indexing from i to i + 1 dp[i+1] = dp[i] + nums[i] where 0 <= i <= n-1 dp[0] = sthspecial
Note that since we only changed the indexing of dp[i], i is still from [0, n-1], so nums table indexing does not need to change
for (int i = 0; i < n; ++i) dp[i+1] = dp[i] + nums[i]