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.

分析

首先边界是dp[0]=nums[0],dp[1]=max(nums[0],nums[1]);对每一个house来说,只有抢与不抢两种情况,故动态转移方程dp[n]=max(dp[n-1],dp[n-2]+nums[n]).

class Solution {
public:
int rob(vector<int>& nums) {
if(nums.size()==0) return 0;
else if(nums.size()==1) return nums[0];
int dp[nums.size()];
dp[0]=nums[0]; dp[1]=max(nums[0],nums[1]);
for(int i=2;i<nums.size();i++)
dp[i]=max(dp[i-1],dp[i-2]+nums[i]);
return dp[nums.size()-1];
}
};

空间复杂度为O(1)的

class Solution {
public:
int rob(vector<int>& nums) {
int a=0,b=0;
for(int i=0;i<nums.size();i++){
if(i%2==0)
a=max(a+nums[i],b);
else
b=max(b+nums[i],a);
}
return max(a,b);
}
};

198. House Robber(动态规划)的更多相关文章

  1. 198. House Robber(动态规划)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

  2. Leetcode 198 House Robber 动态规划

    题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...

  3. 198. House Robber,213. House Robber II

    198. House Robber Total Accepted: 45873 Total Submissions: 142855 Difficulty: Easy You are a profess ...

  4. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  5. 动态规划 - 198. House Robber

    URL : https://leetcode.com/problems/house-robber/ You are a professional robber planning to rob hous ...

  6. [LeetCode] 198. House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  7. (easy)LeetCode 198.House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. 【LeetCode】198 - House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  9. Java [Leetcode 198]House Robber

    题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...

随机推荐

  1. https://github.com/Boris-Em/BEMCheckBox

    https://github.com/Boris-Em/BEMCheckBox BEMCheckBox BEMCheckBox is an open source library making it ...

  2. 【POJ 2054】 Color a Tree

    [题目链接] http://poj.org/problem?id=2054 [算法] 贪心 [代码] #include <algorithm> #include <bitset> ...

  3. openStack Packages yum upgrade

    依赖关系解决 ============================================================================================= ...

  4. Behavior Designer扩展

    BehaviorManager.instance.Tick(behaviorTree); 卸载update里u3d直接卡死 = = SharedVariable直接赋值会改变他的引用关系,必须用XXX ...

  5. Django day25 序列化组件(*****)

    序列化:将变量从内存中存储或传输的过程称之为序列化 1.序列化组件是干什么用的? 对应着表,写序列化的类 2.如何使用序列化组件 Serializer 1) 重命名:用source:xx = seri ...

  6. Vue 页面回退参数被当作字符串处理

    当时情景是这样的,我从A页面跳到B页面时会传一个Boolean类型的参数,当B跳到C,再从C返回B的时候,控制台打印发现参数还在,可是判断怎么都不起作用,后来发现,当页面返回的时候,默认将参数变成了字 ...

  7. SpringBoot SpringDataJPA 动态查询、多条件查询

    Spring-data - JPA用的还是比较多的,之前在公司也一直在用,因为方便.下面我们来整理一下如何配置. pom.xml <?xml version="1.0" en ...

  8. idea git项目修改后无法提交 颜色都是灰色

    现象: idea git项目修改后无法提交   修改后的文件提交时不显示  颜色都是灰色 分析问题出现的原因: git未识别idea下项目. 排查: 发现是因为这个项目之前是基于svn的,然后直接导入 ...

  9. url参数为数组

    //url中state参数为数组 ?baseline_id=12&version_id=34&state[]=complete&state[]=hangup&state ...

  10. JavaScript--Date 日期对象

    日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 定义一个时间对象 : var Udate=new Date(); 注意:使用关键字new,Date()的首字母必须大写. 使 ...