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个不挑出。
class Solution {
public:
int rob(vector<int>& nums) {
if(nums.empty()) return ;
if(nums.size()==) return nums[];
if(nums.size()==) return max(nums[],nums[]); int dp[];
dp[]=nums[]; //初始化也是很重要的
dp[]=max(nums[],nums[]); for(int i=; i<nums.size(); i++)
{
int tmp=max(dp[],dp[]+nums[i]);
dp[]=dp[];//往前移。因为dp[0]已经没作用了
dp[]=tmp;
}
return dp[];
}
};
AC代码
LeetCode House Robber 家庭劫犯(dp)的更多相关文章
- [LeetCode]House Robber II (二次dp)
213. House Robber II Total Accepted: 24216 Total Submissions: 80632 Difficulty: Medium Note: Thi ...
- [LeetCode] House Robber II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [LeetCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- LeetCode House Robber III
原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...
- LeetCode House Robber
原题链接在这里:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to ro ...
- [LeetCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- Leetcode House Robber II
本题和House Robber差不多,分成两种情况来解决.第一家是不是偷了,如果偷了,那么最后一家肯定不能偷. class Solution(object): def rob(self, nums): ...
- LeetCode——House Robber
Description: You are a professional robber planning to rob houses along a street. Each house has a c ...
- 第七周 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的第i位开始匹配2^k个 ...
随机推荐
- Hibernate Annotation (…
引自:http://www.cnblogs.com/hongten/archive/2011/07/20/2111773.html 进入:http://www.hibernate.org 说明文档: ...
- C++11/14的新特性——更简洁
新的字符串表示方式——原生字符串(Raw String Literals) C/C++中提供了字符串,字符串的转义序列,给输出带来了很多不变,如果需要原生义的时候,需要反转义,比较麻烦. C++提 ...
- (转)深度学习(Deep Learning, DL)的相关资料总结
from:http://blog.sciencenet.cn/blog-830496-679604.html 深度学习(Deep Learning,DL)的相关资料总结 有人认为DL是人工智能的一场革 ...
- linux命令之grep,find
grep命令 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索 ...
- 安装wepack
安装webpack之前要安装node.js 1.安装webpack运行 npm install webpack -g 和npm install webpack-cli -g npm install w ...
- Unity T4M 中文讲解
http://blog.csdn.net/tianmao111/article/details/46482963 现在在u3d圈里流行了一种地形转换器(或者叫编辑器吧),但是经查阅之后,似乎还没有中文 ...
- appium运行遇到的坑01
..... File "build\bdist.win32\egg\appium\webdriver\errorhandler.py", line 29, in check_res ...
- maven分层架构搭建
1.准备工作: 1.创建数据源 CREATE TABLE `users` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFA ...
- [转] 【iOS基础知识】之判断NSString是否为整数、浮点数
//判断是否为整形: - (BOOL)isPureInt:(NSString*)string{ NSScanner* scan = [NSScannerscannerWithString:string ...
- 详解window.history
http://blog.csdn.net/woxueliuyun/article/details/51075272