House Robber——LeetCode
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的更多相关文章
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- 算法工程师:双非渣硕是如何获得百度、京东双SP
本人本科硕士皆双非,和牛客大佬们没得比,目前拿到的还可以的offer就是百度SP和京东SP,都是做的推荐算法,其他的不说了. 先说一下个人经历吧,学校比较水,实验室没有项目,实习经历:腾讯实习+滴滴实 ...
- [LeetCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- [LeetCode] House Robber II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [LeetCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- LeetCode House Robber III
原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...
- LeetCode House Robber
原题链接在这里:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to ro ...
- leetcode:House Robber(动态规划dp1)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Leetcode 337. House Robber III
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...
随机推荐
- mcrypt.h not found. Please reinstall libmcrypt
在centos上对php5.6进行源码安装的时候, 出现了如题所示错误提示, 原因是由于centos源不能安装libmcrypt-devel,由于版权的原因没有自带mcrypt的包 解决办法使用php ...
- Python中的map()函数和reduce()函数的用法
Python中的map()函数和reduce()函数的用法 这篇文章主要介绍了Python中的map()函数和reduce()函数的用法,代码基于Python2.x版本,需要的朋友可以参考下 Py ...
- Android开发技巧——去掉TextView中autolink的下划线
我们知道,在布局文件中设置textview的autolink及其类型,这时textivew上会显示link的颜色,并且文字下面会有一条下划线,表示可以点击.而在我们在点击textview时,应用将根据 ...
- html.day01
1.web标准: 1. 结构 (xhtml) 2. 表现(css) 3.行为(js) html 超文本标记语言 xhtml (严格型超文本标记语言) 2.规范: 1. 所有标签(标记)都要 ...
- android自定义listview实现圆角
在项目中我们会经常遇到这种圆角效果,因为直角的看起来确实不那么雅观,可能大家会想到用图片实现,试想上中下要分别做三张图片,这样既会是自己的项目增大也会增加内存使用量,所以使用shape来实现不失为一种 ...
- RESTful Web Services简单介绍
近几年,RESTful Web Services渐渐开始流行,大量用于解决异构系统间的通信问题.很多网站和应用提供的API,都是基于RESTful风格的Web Services,比较著名的包括Twit ...
- (转)ecshop 后台商品分类添加图片的功能
转之--http://blog.sina.com.cn/s/blog_4696b3760100n5ee.html 1 .首先找到数据表 ecs_category (商品分类表) 添加一 cat_i ...
- updatepanel的属性
updatepanel的属性 1.childrenastriggers:内容模板内的子控件的回发是否更新本模板(和updatemode的conditional有关) 2.updatemode:内容模板 ...
- Python os常用模块
Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Wi ...
- drop table xx purge
drop table xx purge; 说明: 所有删除的表都会在回收站里面,只有后面加上purge才是彻底的清空表. (一般用于测试.练习数据表,所以最好不要带purge,要不误删就找不到了.)