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.

题目大意就是,要抢劫一条街,不能抢连续的两家,否则会报警,只能隔着抢或者跳着抢,输出可以抢到的最大值。这属于入门的动规题,可以理解为一个无上限的有限制的01背包题。

我的思路是:

  使用数组F[1...n]表示,抢劫到第i家时的最大获利为F[i],那么根据题意可以写出递推公式F[i]=max{F[i-1],F[i-2]+num[i]},其中num[i]是抢第i家可以获利多少。

下面是递归和非递归两种实现:

int[] max = new int[2000];

    public int rob(int[] num) {

        if (num == null || num.length == 0) {
return 0;
}
if (num.length == 1) {
return num[0];
}
if (num.length == 2) {
return Math.max(num[0], num[1]);
}
Arrays.fill(max, -1);
return choose(num.length - 1, num);
} public int choose(int k, int[] num) {
if (k == 0)
return num[k];
if (k < 0) {
return 0;
}
if (max[k] == -1) {
max[k] = Math.max(choose(k - 1, num), choose(k - 2, num) + num[k]);
}
return max[k];
}

非递归:

 public int rob2(int[] num) {

        if (num == null || num.length == 0) {
return 0;
}
if (num.length == 1) {
return num[0];
}
if(num.length==2){
return Math.max(num[0],num[1]);
}
max[0]=num[0];
max[1]=Math.max(num[0],num[1]);
for(int i=2;i<num.length;i++)
{
max[i]=Math.max(max[i-1],max[i-2]+num[i]);
}
return max[num.length-1];
}

House Robber——LeetCode的更多相关文章

  1. Solution to LeetCode Problem Set

    Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...

  2. 算法工程师:双非渣硕是如何获得百度、京东双SP

    本人本科硕士皆双非,和牛客大佬们没得比,目前拿到的还可以的offer就是百度SP和京东SP,都是做的推荐算法,其他的不说了. 先说一下个人经历吧,学校比较水,实验室没有项目,实习经历:腾讯实习+滴滴实 ...

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

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

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

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

  5. [LeetCode] House Robber 打家劫舍

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

  6. LeetCode House Robber III

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

  7. LeetCode House Robber

    原题链接在这里:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to ro ...

  8. leetcode:House Robber(动态规划dp1)

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

  9. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...

随机推荐

  1. Linux如何关闭防火墙和查看防火墙的具体情况

    1.Linux下关闭和开启防火墙 1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: ser ...

  2. okhttp 基本介绍

    资料汇总 官网:http://square.github.io/okhttp/ 文档:https://github.com/square/okhttp/wiki GitHub:https://gith ...

  3. Android 中Webview 自适应屏幕

    随笔 - 478  文章 - 3  评论 - 113 Android 中Webview 自适应屏幕   webview中右下角的缩放按钮能不能去掉 settings.setDisplayZoomCon ...

  4. 【转】JAVA的StringBuffer类

    [转]JAVA的StringBuffer类    StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBu ...

  5. css 圆角效果

    http://intacto10years.com/index_start.php<div style="width:800px; height:1300px;">&l ...

  6. seajs常用API整理

    本文来自于https://github.com/seajs/seajs/issues/266

  7. Get URL parameters & values with jQuery

    原文: http://jquery-howto.blogspot.jp/2009/09/get-url-parameters-values-with-jquery.html In this post, ...

  8. Activiti 工作流得到最后一次批注的时间

    我们有时候在工作流开发中可能会遇到这样的需求,就是已经审批结束的流程,可能我们还是仍然需要修改业务表的结果,而且我们需要一个时间期限,比如:在5天内可以进行修改 ,这个时候我们就需要得到我们最后一步审 ...

  9. 15 3Sum(寻找三个数之和为指定数的集合Medium)

    题目意思:给一个乱序数组,在里面寻找三个数之和为0的所有情况,这些情况不能重复,增序排列 思路:前面2sum,我用的是map,自然那道题map比双指针效率高,这道题需要先排序,再给三个指针,i.j.k ...

  10. 离线安装maven

    maven离线安装 1.在eclipse根目录下新建两个文件夹,links和myplugins,myplugins文件名可以自定义 2.下载maven http://pan.baidu.com/s/1 ...