1 题目

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.

Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.

Hide Tags

Dynamic Programming

 
2 分析
 
提示是动态规划,也就是要先写好递推方程。好吧,我自己想了半天想不出来,总是有问题。看了别人的代码之后就想明白了。递推方程是:cache[i] = Max[cache[i-1] + num[i],cache[i-1]];  取前者时,肯定没有相邻的房间,而去后者,就不rob房间i,前面的rob的房间序列也是满足条件的,所以该递推方程有效。
 
3 代码
     public int rob(int[] num) {
int n = num.length;
if (n < 2)
return n == 0 ? 0 : num[0];
int[] cache = new int[n];
cache[0] = num[0];
cache[1] = num[0] > num[1] ? num[0] : num[1];
for (int i = 2; i < n; i++) {
cache[i] = cache[i - 2] + num[i];
cache[i] = cache[i] > cache[i-1]? cache[i] : cache[i-1];
}
return cache[n - 1]; }

[leet code 198]House Robber的更多相关文章

  1. 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:二叉树下的不能相邻,求能 ...

  2. 198. House Robber,213. House Robber II

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

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

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

  4. 【Leet Code】Palindrome Number

    Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...

  5. Leet Code 771.宝石与石头

    Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S  ...

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

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

  7. Leetcode 198 House Robber

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

  8. Java for LeetCode 198 House Robber

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

  9. (easy)LeetCode 198.House Robber

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

随机推荐

  1. DP:0

    小故事: A * "1+1+1+1+1+1+1+1 =?" * A : "上面等式的值是多少" B : *计算* "8!" A *在上面等式 ...

  2. Java开发环境安装配置

    电脑配置:Win7 64位 JDK1.8+Apache Tomcat8.5.4 下载JDK1.8 全名: Java SE Development Kit 8u151,下载最新稳定版本 下载地址:htt ...

  3. pygame小记

    pygame.display.set_mode(x, y)设置显示窗口大小pygame.sprite.Sprite方法中有image, rect, speed等参数 其中image 可以通过 pyga ...

  4. OPNET仿真软件资料合集

    1. OPEN中国代理商业 http://www.credit-top.com/page/Default.asp?pageID=105

  5. java.lang.SuppressWarnings的注解简介

    简介:java.lang.SuppressWarnings是J2SE 5.0中标准的Annotation之一.可以标注在类.字段.方法.参数.构造方法,以及局部变量上.作用:告诉编译器忽略指定的警告, ...

  6. 783. Minimum Distance Between BST Node

    方法一,非递归方法,中序遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *l ...

  7. 2018.12.15 spoj Substrings(后缀自动机)

    传送门 后缀自动机基础题. 求长度为iii的子串出现次数的最大值. 对原串建出samsamsam,然后用sizsizsiz更新每个maxlenmaxlenmaxlen的答案. 然后由于后缀链接将其转化 ...

  8. Tomcat架构解析(一)-----Tomcat总体架构

    Tomcat是非常常用的应用服务器,了解Tomcat的总体架构以及实现细节,对于理解整个java web也是有非常大的帮助. 一.Server   1.最简单的服务器结构 最简单的服务器结构如图所示: ...

  9. (12)We should aim for perfection — and stop fearing failure

    https://www.ted.com/talks/jon_bowers_we_should_aim_for_perfection_and_stop_fearing_failure/transcrip ...

  10. HTTP 错误 500.XX - Internal Server Error 解决办法

    HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息 模块 IIS Web Core 通知 未知 处理程序 尚未 ...