标签:dp house robber leetcode 动态规划
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
题目意思:假如现在你是一个强盗,一个武功超群,足智多谋的江洋大盗,现在你要去一条街上抢劫,这条街全是贪官污吏,一共有N家,每家都有一定数量的金子,如果相邻的两家在同一个晚上都被打劫了,那么就会有一个类似触发器的东西自动调动兵马来街上抓你。
请问,在不触发这个警报器的前提下,你能抢劫到多少money?
动态规划思想解题
分析:,有N家贪官,假设是从左到右,第i家贪官家里的钱数为m[i],i从0到N-1,根据题意可知,肯定不能在今晚打劫两个相邻的贪官,也就是假如打劫了第i家,就不能打劫第i-1家和i+1家。
设dp[i]表示我从第1家到达第i家能强盗的最大money数;
所以最后我们要得到的就是dp[N-1],即到达最后一家时的最大money数。
Code(c++):
class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if(n == 0) return 0;
vector<int> dp(n);
for(int i = 0; i < n; i++) {
if(i == 0) dp[i] = nums[i];
else if(i == 1) dp[i] = max(nums[i],nums[i-1]);
else{
dp[i] = max(dp[i-1], dp[i-2] + nums[i]);
}
}
return dp[n-1];
}
};
标签:dp house robber leetcode 动态规划
原文地址:http://blog.csdn.net/dream_angel_z/article/details/46490023