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. POJ 1384 Piggy-Bank(完全背包)

    Description Before ACM can do anything, a budget must be prepared and the necessary financial suppor ...

  2. AsyncTask应用示例

    package com.example.testdemo; import java.io.ByteArrayOutputStream; import java.io.IOException; impo ...

  3. Linux mii-tool 命令

    mii-tool 用来查看或设置网卡的相关参数,该命令已经过时了,推荐使用 ethtool 命令 [root@localhost ~]$ mii-tool -v eth1 # 查看网卡的相关信息,包括 ...

  4. 运行 3ds Max 时出现的性能问题

          运行 3ds Max 时性能减慢或迟缓通常是由于视频配置冲突或内存分配问题引起的.关于性能问题的一大难点在于缩小范围确定问题原因.以下是一些限制 3ds Max 操作的常见情形,以及纠正这 ...

  5. 一次Win10安装体验

    我下载的是win10 Build 14279版本.http://www.iwin10.com/xiazai/1071.html 下载之后就直接拷到U盘安装了. 安装完之后发现(因为我是分区成了两个)我 ...

  6. c++11实现optional

    optional< T> c++14中将包含一个std::optional类,optional< T>内部存储空间可能存储了T类型的值也可能没有存储T类型的值.当optiona ...

  7. LeetCode——Balanced Binary Tree

    Description: Given a binary tree, determine if it is height-balanced. For this problem, a height-bal ...

  8. Linux学习之批量修改文件名

    1. 通过专业的改名命令rename实现 [root@oldboy oldboy]# ll total -rw-r--r-- root root Nov : stu_102999_1_finished ...

  9. 【黑金ZYNQ7000系列原创视频教程】06.ZYNQ来自FPGA的中断——按键中断实验

    黑金论坛地址: http://www.heijin.org/forum.php?mod=viewthread&tid=36640&extra=page%3D1 爱奇艺地址: http: ...

  10. Windows Phone 7 程序等待页面的处理

    程序启动通常会有一个等待的过程,在这个过程中可以通过使用Popup控件配合BackgroundWorker类启动后台线程来实现. 控件的代码 PopupSplash.xaml <UserCont ...