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. Java 四大域对象总结

    一.ServletContext 1.生命周期:当Web应用被加载进容器时创建代表整个web应用的ServletContext对象,当服务器关闭或Web应用被移除时,ServletContext对象跟 ...

  2. Hadoop 1、在虚拟机上进行 HDFS 安装

    一.准备条件 1.四台Linux虚拟机(1台NameNode节点,1台Secondary节点(Secondary和其中1台DataNode共用),外加2台DataNode) 2.下载Hadoop版本, ...

  3. (LeetCode)两个队列来实现一个栈

    原题例如以下: Implement the following operations of a stack using queues. push(x) -- Push element x onto s ...

  4. HDU2111 Saving HDU 【贪心】

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. IP转发和子网路由

    IP地址的分类 在TCP/IP协议中,协议栈分为4层.从上到下依次是应用层.运输层.网络层.网络接口层. IP协议就工作在网络层.IP协议将纷繁复杂的物理层协议屏蔽掉,对上层提供统一的描述和管理服务. ...

  6. List.removeAll()方法失效

    List.removeAll()方法失效 前几天遇到List.removeAll()方法失效,测试了半天都没测出来,后面跟老大在那边调试了半天,最后终于找出原因,以后要是谁遇到这个奇葩的问题可以借鉴参 ...

  7. ListView小坑

    ListView的addHeaderView()和addFooterView()方法需要“Call this before calling setAdapter”,否则崩溃. 但是在KITKAT(ap ...

  8. 跳转UICollectionViewController报Could not load NIB in bundle解决办法

    报错代码如下:'Could not load NIB in bundle: 'NSBundle </Users/mac/Library/Developer/CoreSimulator/Devic ...

  9. poj2251 三维简单BFS

    D - (热身)简单宽搜回顾 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  10. Python代码一定要对齐

    不然会出现错误: IndentationError: unindent does not match any outer indentation level PS:新的Python语法,是不支持的代码 ...