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 ...
随机推荐
- getconf 取系统配制 --CPU
cpu 缓存:getconf -a LEVEL1_ICACHE_SIZE LEVEL1_ICACHE_ASSOC LEVEL1_ICACHE_LINESIZE LEVEL1_DCACHE_SIZE L ...
- diy 电脑 重装系统
1\组装好电脑 2\硬盘的红灯只亮一会,找不到 硬盘 .bios里 硬盘设置里 IDE修改为 ACHI即可. 3\老毛桃 \ 用桌面的那个分区软件 分区 100G,300G 4\找到U盘 的iso 文 ...
- android 用代码画虚线边框背景(转)
1.虚线画效果,可以使用Android中的xml来做. 2.直接上代码: <RelativeLayout android:id="@+id/coupon_popup" and ...
- uva 11324 The Largest Clique (Tarjan+记忆化)
/*每个环 要么不选 要么全选 可缩点 就得到一个GAD图 然后搞搞算出最大路径*/ #include<iostream> #include<cstdio> #include& ...
- asp.net,mvc4,mysql数据库,Ef遇到问题集合
asp.net mvc mysql数据库,在我一个新手自学MVC4时遇到如下的问题,一一解决掉的方法记录如下方便自己日后查看,也为了方便一些像我一样的新手遇到如下问题时,提供参考 问题一: 解决办 ...
- Hibernate中分页
query.setFirstResult(4);query.setMaxResults(5); 这两个方法就是hibernate的分页
- java学习——IO流
字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表.获取对应的文字.在对这个文字进行操作.简单说:字节流+编码表 ---------------------------- ...
- ajax无刷新方式收集表单并提交表单
ajax无刷新方式收集表单有两种方式, 一个是使用html5的FormData.一个是传统的方式. 一,FormData,在主流的浏览器中可以用,IE不好用啊. 另外,FormData使用有两个条件, ...
- 【USACO 2.3.4】货币系统
[描述] 母牛们不但创建了它们自己的政府而且选择了建立了自己的货币系统.由于它们特殊的思考方式,它们对货币的数值感到好奇. 传统地,一个货币系统是由1,5,10,20 或 25,50, 和 100的单 ...
- 『重构--改善既有代码的设计』读书笔记----Inline Method
加入间接层确实是可以带来便利,但过多的间接层有时候会让我自己都觉得有点恐怖,有些时候,语句本身已经够清晰的同时就没必要再嵌一个函数来调用了,这样只会适得其反.比如 void test() { if ( ...