198. 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 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.

Example 1:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
  Total amount you can rob = 1 + 3 = 4.

Example 2:

Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
  Total amount you can rob = 2 + 9 + 1 = 12.

如果两个相邻的房子在同一个晚上被打破,它将自动联系警察。

 class Solution:
def rob(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
if n==0:
return 0
if n==1 :
return nums[0]
dp=[0]*n
dp[0] =nums[0]
dp[1] = max(nums[0],nums[1])
for i in range(2,n):
dp[i] = max(dp[i-2]+nums[i],dp[i-1])
return dp[n-1]
 class Solution {
public:
int rob(vector<int>& nums) {
int n = nums.size();
if(n==) return ;
if(n==) return nums[];
if(n==) return std::max(nums[],nums[]);
vector<int> dp(n,);
dp[] = nums[];
dp[] = std::max(nums[],nums[]);
for(int i = ;i<n;i++)
dp[i] = std::max(dp[i-],nums[i]+dp[i-]);
return dp[n-];
}
};

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

  1. Leetcode 198 House Robber 动态规划

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

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

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

  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. swift--使用UserDefaults来进行本地数据存储

    UserDefaults适合轻量级的本地客户端存储,存储一个值,新值可以覆盖旧值,可以重复存储,也可以存储一次,然后直接从UserDefaults里面读取上次存储的信息,很方便,用的时候,宏定义下,直 ...

  2. Swift学习笔记之--类和对象

    通过在 class后接类名称来创建一个类.在类里边声明属性与声明常量或者变量的方法是相同的,唯一的区别的它们在类环境下.同样的,方法和函数的声明也是相同的写法 class Shape { func s ...

  3. Python 解压缩Zip和Rar文件到指定目录

    #__author__ = 'Joker'# -*- coding:utf-8 -*-import urllibimport osimport os.pathimport zipfilefrom zi ...

  4. 当JS出现的Cannot read property 'XXX' of null错误

    由于在加载JS的时候,页面还未加载完成,就出现了这样的错误.解决方法很简单,将这段 js 放到页面的最下面,等到所以页面加载完成时,再加载这段JS.

  5. 【python系列】SyntaxError:Missing parentheses in call to 'print'

    打印python2和python3的区别 如上图所示,我的 PyCharm安装的是python3.6如果使用print 10会出现语法错误,这是python2.x和python3.x的区别所导致的.

  6. JS-简单地匀速运动框架

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. Splay模板 1.0

    struct Splay{ int rt,sz; ///根节点,树节点总数 ],fa[N];///值,左右儿子,父亲 void spin(int t){ ///旋转操作 ]==t; son[x][y] ...

  8. mysql如何查询日期的列表?

    转自:http://blog.csdn.net/liufei198613/article/details/72643345 select @num:=@num+1,date_format(adddat ...

  9. Centos6.8 防火墙设置

    1.指令 vi /etc/sysconfig/iptables 添加以下内容和要开放的端口 # Firewall configuration written by system-config-fire ...

  10. 网站微图标,页标签,favicon.ico

    随便打开一个网页:比如 http://www.baidu.com/ 可以看到在浏览器的标签头上面显示了一个图标,也就是我们常说的favicon.ico, 由于这篇文章主要讨论favicon.ico,以 ...