LeetCode House Robber 家庭劫犯(dp)

题意:有一个整数序列,从中挑出一些数字,使得总和是最大,前提是,相邻的两个数字中只能挑其一。比如1 2 3 就只能挑2或者1和3。

思路:很直观的题,dp思想。降低规模,从小规模开始考虑。如果只有两个数字,那么结果很明显就能知道是其中之大者。假如已经知道了第 i 个之前的决策,那么第i+2个之前的决策也就知道了。前两个数字已经由人工得知,设为dp[0]和dp[1],那么dp[2]=max(dp[0]+nums[2], dp[1])。状态转移方程dp[i]=max(dp[i-1], dp[i-2]+num[i] )。

这里有状态压缩的思想,只不过状态只有两个,0和1代表前一个数字是否被挑出。即dp数组的下标,1代表i-1个之前的决策结果,也代表了第i-1个已经挑出,所以第i个不能再挑出来了;但是0代表i-2个之前的决策结果,也代表了i-1个不挑出。

 1 class Solution {
 2 public:
 3     int rob(vector<int>& nums) {
 4         if(nums.empty())    return 0;
 5         if(nums.size()==1)    return nums[0];
 6         if(nums.size()==2)    return max(nums[1],nums[0]);
 7
 8         int dp[2];
 9         dp[0]=nums[0];    //初始化也是很重要的
10         dp[1]=max(nums[0],nums[1]);
11
12         for(int i=2; i<nums.size(); i++)
13         {
14             int tmp=max(dp[1],dp[0]+nums[i]);
15             dp[0]=dp[1];//往前移。因为dp[0]已经没作用了
16             dp[1]=tmp;
17         }
18         return dp[1];
19     }
20 };

AC代码

时间: 2024-10-29 10:46:31

LeetCode House Robber 家庭劫犯(dp)的相关文章

LeetCode—House Robber 寻找数组不相邻组合最大值DP

https://leetcode.com/problems/house-robber/ 题目设计了一个抢劫犯的情景,其实就是求数组中不相邻数据进行组合得到的最大值 举一个例子 假设数据: 8 3 6 15 4 9 7 10 那么首先可能选取 8 , 3 每一个数字的选取都是根据他的前两个数字,前三个数字得到的最大值进行选择,等到6的时候考虑前面只能和8组合  8,3,14 到数字15,那么就可以考虑在8,3中进行组合  8,3,14,23,4,9,7,10 接下来的步骤: 8,3,14,23,1

LeetCode House Robber III

原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent hous

[LeetCode] House Robber

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 autom

LeetCode -- House Robber II

Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. 

第七周 Leetcode 466. Count The Repetitions 倍增DP (HARD)

Leetcode 466 直接给出DP方程 dp[i][k]=dp[i][k-1]+dp[(i+dp[i][k-1])%len1][k-1]; dp[i][k]表示从字符串s1的第k位开始匹配2^k个s2串需要的长度 最后通过一个循环 累积最多可以匹配多少个s2串 除以n2下取整就是答案 用倍增加速后 总的复杂度nlogn 而本题的n非常小 轻松AC 体会到倍增的魅力了吧. const int maxn=100+1,INF=1e+9; long long int dp[maxn][30]; cl

LeetCode——House Robber

Description: 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

[LeetCode] House Robber II 打家劫舍之二

Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle.

[LeetCode] House Robber 打家劫舍

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 autom

[LeetCode] 22. 括号生成(回溯/DP)

题目 给出?n?代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出?n = 3,生成结果为: [ "((()))", "(()())", "(())()", "()(())", "()()()"] 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/generate-parentheses 著作权归领扣网络所有.商