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): ...
随机推荐
- PHP常用魔术方法(__set、__get魔术方法:)
__set.__get魔术方法: //文件名:Object.php <?phpnamespace IMooc;class Object{ protected $array = array(); ...
- UIActivityIndicatorView-初识IOS
UIActivityIndicatorView是一个加载动画的视图,一般加载一个网页页面之前会经常用到. 上一个随笔,我讲到了页面加载的页面的那些代理方法 - (void) viewWillAppea ...
- .NET基础拾遗(3)字符串、集合和流3
三.流和序列化 3.1 流概念及.NET中常见流 无论什么信息,文字,声音,图像,只要进入了计算机就都被转化为数字,以数字方式运算.存储.由于计算机中使用二进制运算,因此数字只有两个:0 与 1,就是 ...
- UTF8转GB2312(UTF8解码)
小弟C++上手没多久,代码不严谨之处敬请见谅.英语也不是很好,有的是直接使用的拼音. string MyUTF_8toGB2312(string str) { ,,str.c_str(),-,NULL ...
- C#重写Equals方法步骤
检查传入的参数是否为null, 如果为null,那么返回false, 否则执行步骤2 调用ReferenceEquals查看是否为统一个对象,如果是,那么返回true, 否则执行步骤3 判断两者是否为 ...
- 浅谈js闭包
相信很多人只知道闭包这个词但是具体是怎么回事就不太清楚了,最近在群里有很多小伙伴讨论这个问题但还是蒙眬眬的赶脚.索性就写了这篇文章来帮助大家一起理解闭包. 变量作用域 闭包其实想明白了很简单,但是在理 ...
- 关于Jquery.validate.js中动态删除验证remove方法的Bug
利用Jquery.validate.js 来做动态验证的时候,需要特定的情况下,删除添加opAmount的必须入力的Check $("#form").validate({ rule ...
- OpenGL ES 2.0 绘制方式
OpenGL ES 中支持的绘制方式大致分3类,包括点.线段.三角形,每类中包括一种或多种具体的绘制方式. GL_POINTS 传入渲染管线的一系列顶点单独进行绘制. GL_LINES 传入渲染管 ...
- memcached真实项目中的应用
上一篇memcached介绍及基本使用介绍了memcached的一些基本概念和一个范例.这一篇将介绍一个memcached在实际项目中的应用
- 【5】说说Laravel5的blade模板
首先看一下以前的程序 routes.php PagesController.php resources/views/pages/about.blade.php 现在我们来简单的使用一下blade模板的 ...