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. Android ActionBar(转)

    本文内容 关于 ActionBar 必要条件 项目结构 环境 演示一:Action Bar 显示隐藏 演示二:Action Item 显示菜单选项 演示三:Action Home 启用“返回/向上”程 ...

  2. JNI与多线程

    在android开发过程中,由于主线程要聚焦于UI交互,为了软件运行流畅必然要用到很多多线程技术.而在JNI机制中专门提供了一些避免线程冲突的函数.了解.学习并掌握如何避免线程冲突问题是一个程序猿的必 ...

  3. Android Studio学习随笔-基本事件(点击)

    最常见的点击事件有三种创建方法,在MainActivity.java的onCreate函数(在启动程序是优先运行的程序)中创建setOnClickListener(动态运行)(最常见) protect ...

  4. mysql 大表 Sharding [转]

    参看以下两篇文章 http://www.dedecms.com/knowledge/data-base/mysql/2012/0820/9172.html http://dbanotes.net/da ...

  5. sql的一些小东西

    1.sa账户密码丢失. 先用wiondows验证登陆,然后新建查询 “ ALTER LOGIN [sa] WITH PASSWORD = N'NewPassword' ”

  6. JSON和JSONP区别

    JSON(JavaScript Object Notation)和JSONP(JSON with Padding) JSON是一种数据交换格式,JSONP是一种跨域数据交互协议 JSONP利用scri ...

  7. php SESSION 不能跨页面传递

    今天想用一个session来实现用户登录判断,也算是对之前session的探究,查了下资料session的运行机制如下: session是服务器端的一种会话机制,当客户端的请求服务器创建一个sessi ...

  8. java编程思想-注解思维导图

  9. Google与微软为jQuery等类库提供的CDN服务

    相关链接: Google:  http://code.google.com/apis/ajaxlibs/Microsoft:  http://www.asp.net/ajaxlibrary/cdn.a ...

  10. Android开发手记(23) Notification

    有时候,我们需要应用程序在状态内显示一些通知信息,这时我们就需要使用Notification来完成这一工作.也许我们会想到以前经常使用的Toast来通知用户.虽然Notification与Toast都 ...