[LeetCode] 198. House Robber
思路:DP,dp[i]表示到第i个房子的最大值。
1 class Solution { 2 public: 3 int rob(vector<int>& nums) { 4 int len = nums.size(); 5 if (len == 0) 6 return 0; 7 if (len == 1) 8 return nums[0]; 9 vector<int> dp(len, 0); 10 dp[0] = nums[0]; 11 dp[1] = max(nums[0], nums[1]); 12 for (int i = 2; i < len; i++) 13 dp[i] = max(nums[i] + dp[i-2], dp[i-1]); 14 return dp[len-1]; 15 } 16 };