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): ...
随机推荐
- Android apk获取系统权限
Android在apk内部,即通过java代码来进行修改系统文件或者修改系统设置等等,这样需要获取系统权限. 通过直接配置apk运行在System进程内 1. 在应用程序的AndroidManifes ...
- 7 个改变世界的 Java 项目
英文链接:http://radar.oreilly.com/2011/07/7-java-projects.html 文章转载自:开源中国社区 http://www.oschina.net/news/ ...
- input文本框获取焦点和失去焦点判断
onBlur:当输入框失去焦点后 onFocus:当输入框获得焦点后 这两个JavaScript事件是写在html标签中的例如: <input type="text" onB ...
- js练习【DOM操作】
完成效果: 演示地址:http://codepen.io/anon/pen/jPbYWq HTML: <!DOCTYPE html> <html lang="en" ...
- struts1配置文件之input
<action path="/*Person" type="cn.itcast.PersonAction" scope="request&quo ...
- JS高级程序设计学习笔记之RegExp类型
创建正则表达式: 字面量形式定义正则表达式: Var expression = / pattern /flags ;pattern部分可以使任意简单或复杂的正则表达式.每个正则表达式可以带有一个或多个 ...
- javascript解决for循环中i取值的问题(转载)
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 视图中的Layout使用(转)
1.母板页_Layout.cshtml 类似于传统WebForm中的.master文件,起到页面整体框架重用的目地 1.母板页代码预览 1 <!DOCTYPE html> 2 <ht ...
- AngularJS的$http服务的应用
$http有很多参数和调用方法,本文只记录比较常用的应用及参数. $http 服务:只是简单封装了浏览器原生的XMLHttpRequest对象,接收一个参数,这个参数是一个对象,包含了用来生成HTTP ...
- Bootstrap 字形图标(Glyphicons)
http://w3c.3306.biz/bootstrap/eg/bootstrap--glyphicons-list.html