LeetCode198 House Robber
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的更多相关文章
- LeetCode198 House Robber(打家劫舍)
题目 You are a professional robber planning to rob houses along a street. Each house has a certain amo ...
- [Swift]LeetCode198. 打家劫舍 | House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 简单动态规划-LeetCode198
题目:House Robber You are a professional robber planning to rob houses along a street. Each house has ...
- [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
题目简述 You are a professional robber planning to rob houses along a street. Each house has a certain a ...
- LeetCode House Robber III
原题链接在这里:https://leetcode.com/problems/house-robber-iii/ 题目: The thief has found himself a new place ...
- Leetcode House Robber II
本题和House Robber差不多,分成两种情况来解决.第一家是不是偷了,如果偷了,那么最后一家肯定不能偷. class Solution(object): def rob(self, nums): ...
随机推荐
- 学习phpcms模板方法:
1.改官方模板,读里面的代码,改改它,看看有什么变化,如果不明白,去官方论 坛.查手册.专业人士还可以看数据库.2.复制实例代码,整理笔记,到实战的时候,就直接复制,改改参数即可.
- JavaScript 精髓整理篇之一(对象篇)postby:http://zhutty.cnblogs.com
废话篇头: 由于工作关系,所以写博文的时间有那么点~~,其实是输入法太懒了,都是输入法的错~~ 这一系列的博客将总结所有关于JavaScript语言的精髓,适合0基础到大师级别人物阅读. <Ja ...
- [RxJS] Refactoring Composable Streams in RxJS, switchMap()
Refactoring streams in RxJS is mostly moving pieces of smaller streams around. This lessons demonstr ...
- Div与table的区别
1:速度和加载方式方面的区别 div 和 table 的差异不是速度,而是加载方式,速度只能是指网络速度,如果速度足够快,是没有差异的: div 的加载方式是即读即加载,遇到 <div> ...
- 卸载rpm包提示:error: specifies multiple packages
–allmatches Remove all versions of the package which match PACKAGE_NAME. Normally an error is issue ...
- Sqlserver统计语句
--查看被缓存的查询计划 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED st.text AS [SQL] , cp.cacheobjtype , c ...
- 重写OnPaint事件对窗体重绘 实例1
public class WindowOne:Form { public WindowOne() { InitializeCompnent(); } public void InitializeCom ...
- c# 取得扩展名
string KZM=files[0].FileName.Substring(files[0].FileName.LastIndexOf(".") + 1);
- 利用MiddleGen-hibernate-r5生成hbm文件及POJO文件
1 先决条件 1.1 已安装JDK(版本1.5以上)并配置环境变量 到http://java.sun.com上下载JDK,配置环境变量(我的电脑右键->属性->高级-&g ...
- show,hide与fadeIn、fadeOu的区别
show,hide——>是通过改变height,width,opacity来实现动画的显示与隐藏的 fadeIn.fadeOut——>只通过opacity来实现动画的显示与隐藏的 它们两个 ...