Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 198 House Robber - Base Cases

Starting conditions

You need to define dp[0] and dp[1]. dp[0] tells you the answer for 00 to 00, which means no number is present to choose, so dp[0] = 0.

When you consider dp[1], you have to consider from 00 to 11, the only possible value is a[1]. You can either take it or not take it. The best is to take it, so dp[1] = a[1].

With these two base cases in place, you can fill the rest of the table using the recurrence from the previous unit. The final answer sits at dp[n], covering the full range from index 00 to nn.