LeetCode 198, 213 House Robber

198 House Robber

DP

0~n-1     ans=dp[n-1]

dp[i] = max(dp[i-2]+nums[i], dp[i-1])  i>=2

213 House Robber II

Since we cannot rob nums[0] and nums[n-1] at the same time, we can divide this problem into two cases:

not rob nums[0]

not rob nums[n-1]

and the answer is the maximum one.

C++ stl vector可以用 vector<int> nums1(nums.begin(), nums.end())来初始化 比较实用

猜你喜欢

转载自www.cnblogs.com/hankunyan/p/8874287.html
今日推荐