topic:
Thought:
这一次终于懂一点Dynamic planning了,The first is the youngest problem,I first thought it was to consider where to start,Start from the first or the second one(这刚好是和Hiccup1Difference)。
但实际上最小子问题还是和Hiccup1Same:Whether to rob the current house。
First we set onedp
Array,dp[i]
Indicate robbery to the firsti
The maximum return when a house。
Then we rob the current house after we rob the current house dp[i] = dp[i-2] + nums[i]
,(Because the house can not be robbed before the current house is robbed),Do not rob the current house dp[i] = dp[i-1]
。
So we can get the state transfer equation:dp[i] = max(dp[i-2] + nums[i], dp[i-1])
。
Last classification discussion and robbery, the first or the second start。
Code:
1 | class Solution: |