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. 我的Android进阶之旅------&gt;Android关于ImageSpan和SpannableString的初步了解

    近期要实现一个类似QQ聊天输入框.在输入框中能够同一时候输入文字和表情图像的功能.例如以下图所看到的的效果: 为了实现这个效果.先去了解了一下ImageSpan和SpannableString的使用方 ...

  2. Bing Maps进阶系列六:使用Silverlight剪切(Clip)特性实现Bing Maps的迷你小地图

    Bing Maps进阶系列六:使用Silverlight剪切(Clip)特性实现Bing Maps的迷你小地图 Bing Maps Silverlight Control虽然为我们提供了简洁.方面的开 ...

  3. [BZOJ 2006] 狼抓兔子

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1001 [算法] 最小割 [代码] #include<bits/stdc++.h ...

  4. 【HDU 4864】 Task

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4864 [算法] 贪心 不妨将两个数组分别按x从大到小排序 然后枚举每件物品,选择x值大于该物品的且 ...

  5. assistant: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by

    [oracle@oracledb button]$ assistantassistant: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' no ...

  6. 关于二分查找 使用 lower_bound

    在寻找单调递增最长自序列 , 的时候能不能确认出来哪个是单调递增最长自序列  ?  我的想法是 if(location>=num) dp[location]=b; 这样的 , 基于http:// ...

  7. [转]linux之初识SELinux

    转自:http://www.linuxidc.com/Linux/2014-07/104447.htm 1.selinux的概述 selinux相信大家一定不会陌生,它的全称是内核级加强型防火墙.在服 ...

  8. 【转】linux下vi命令大全

    转自:http://www.cnblogs.com/88999660/articles/1581524.html 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi ...

  9. dubbo之本地伪装

    本地伪装 本地伪装 1 通常用于服务降级,比如某验权服务,当服务提供方全部挂掉后,客户端不抛出异常,而是通过 Mock 数据返回授权失败. 在 spring 配置文件中按以下方式配置: <dub ...

  10. HDU_1027_Ignatius and the Princess II_全排列

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...