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. 可读性很强的C语言的函数指针定义

    通常C/C++程序里面要用到大量的指针,其语法非常难以阅读.比如下面的vp指针类型: #include <iostream> using namespace std; typedef vo ...

  2. Django报错:__init__() missing 1 required positional argument: 'on_delete'

    原因: 在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:TypeError: __init__() missing ...

  3. [Spark]What's the difference between spark.sql.shuffle.partitions and spark.default.parallelism?

    From the answer here, spark.sql.shuffle.partitions configures the number of partitions that are used ...

  4. 【Tomcat】Tomcat + Memcached 实现session共享

    概述 web项目中,Tomcat的访问量总是有限的,这时候就需要用到Tomcat集群,多个Tomcat的时候就要考虑Session共享的问题,这里介绍一种使用Memcached做Session共享的解 ...

  5. canvas 实现烟花效果

    一:创建画布 <canvas width="600" height="600" id="canvas" style="bor ...

  6. 2019.01.20 bzoj5158 Alice&Bob(拓扑排序+贪心)

    传送门 短代码简单题. 题意简述:对于一个序列XXX,定义其两个伴随序列a,ba,ba,b,aia_iai​表示以第iii个数结尾的最长上升子序列长度,bib_ibi​表示以第iii个数开头的最长下降 ...

  7. hdu6351 2018 Multi-University Training Contest 5 1002 Beautiful Now

    题意: 给出一个十进制数,数位两两可以交换,给出最多能交换多少次,以及交换后的数不能有前缀0,问能形成最小和最大的数 * * * 尝试的思路 贪心,将字符串先排出最大以及最小的情况,然后按一定顺序将对 ...

  8. 1.2.2实现Runnable接口

    使用Runnable创建线程 package com.cky.runner; /** * Created by chenkaiyang on 2017/12/2. */ public class My ...

  9. share pool 管理机制

    Library cache是Shared pool的一部分,它几乎是Oracle内存结构中最复杂的一部分,主要存放shared curosr(SQL)和PLSQL对象(function,procedu ...

  10. slice()

    提取字符串中的一部分,并返回这个新的字符串 str.slice(beginSlice[, endSlice]) 参数 beginSlice 从该索引(以 0 为基数)处开始提取原字符串中的字符.如果值 ...