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.

这个题用动态规划。一条街上人家都有一定量的财产,如果有连着两家的东西被盗就会触发警报。如果你去偷一条街的东西,如何在不触发警报的情况下,得到更多的东西。求得动态公式就是:i是到i为止得到的最多的财产。那么取得方法就是,拿第i家和第i-2家的东西(dp[i-2]+nums[i]),或者不拿i家,拿i-1家的东西(dp[i-1)。比较哪个更多一些。

 public class HouseRobber198 {
public int rob(int[] nums){
if(nums == null || nums.length ==0){
return 0;
} int[] dp = new int[nums.length +1];
dp[0] = 0;
dp [1] = nums[0]; for(int i = 2; i <=nums.length; i++){
dp[i] = Math.max(dp[i-2]+nums[i-1], dp[i-1]);
} return dp[nums.length];
}
}

LeetCode198 House Robber的更多相关文章

  1. LeetCode198 House Robber(打家劫舍)

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

  2. [Swift]LeetCode198. 打家劫舍 | House Robber

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

  3. 简单动态规划-LeetCode198

    题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...

  4. [LeetCode] House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  5. [LeetCode] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  6. [LeetCode] House Robber 打家劫舍

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

  7. 【leetcode】House Robber

    题目简述 You are a professional robber planning to rob houses along a street. Each house has a certain a ...

  8. LeetCode House Robber III

    原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...

  9. Leetcode House Robber II

    本题和House Robber差不多,分成两种情况来解决.第一家是不是偷了,如果偷了,那么最后一家肯定不能偷. class Solution(object): def rob(self, nums): ...

随机推荐

  1. 哲学家用餐问题的几个解法(c语言实现)

    参考资料: 1.维基百科:哲学家用餐问题 2.Windows的多线程编程

  2. PHP常用魔术方法(__call魔术方法:)

    魔术方法  __call <?php //文件名:index.php define('a',__DIR__); include '/IMooc/Loader.php'; spl_autoload ...

  3. python ctypes小例子

    import time import ctypes import ctypes.wintypes SEE_MASK_NOCLOSEPROCESS = 0x00000040 SEE_MASK_INVOK ...

  4. AngularJs学习笔记3——自定义指令

    指令 概述: 前面也说过一些常用指令,用于快速入门.现在详细总结一下:指令用于实现各种页面的操作,是对于底层DOM操作的封装,扩展了HTML的行为,实现页面交互以及数据绑定. 指令是一种执行的信号,一 ...

  5. 不可视对象的自己主动实例化BUG

    PB有个隐藏BUG会占用内存.影响效率. 先来做个样例吧 (1)创建一个不可视对象n_base,勾选Autolnstantiate属性 初始化事件constructor里面写messagebox('c ...

  6. [RxJS] Logging a Stream with do()

    To help understand your stream, you’ll almost always want to log out some the intermediate values to ...

  7. 初识QML学习机制

    在QML中,一个用户界面被指定为具有属性的对象树,这使得Qt更加便于很少或没有编程经验的人使用,JavaScript在QML中作为一种脚本语言,对QML进行逻辑方面的编程. AD:WOT2015 互联 ...

  8. Linux 常用系统命令-20160504

    一.显示目录和文件的命令 1.ls(list)  功能说明: 列出目录内容. 语 法 : ls [-1aAbBcCdDfFgGhHiklLmnNopqQrRsStuUvxX][-I < 范 本 ...

  9. vsim生成VCD波形文件(verilog)

    Vsim(ModelSim)生成VCD波形文件(verilog) 两种方法 方法一: 调用ModelSim自己的命令生成,仿真脚本中加入如下一句即可 vcd file mytb.vcd 方法二: 调用 ...

  10. js数字精度丢失

    http://www.cnblogs.com/snandy/p/4943138.html