(easy)LeetCode 198.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.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
方法一:递归(超时)
代码如下:
public class Solution {
public int rob(int[] nums) {
int m1= robWay(nums,0);
int m2= robWay(nums,1);
int max=m1>m2?m1:m2;
return max;
}
public int robWay(int[] nums,int begin){
int len=nums.length;
if(begin==len-1) return nums[begin];
if(begin>len-1) return 0;
int m1=nums[begin]+robWay(nums,begin+2);
int m2=nums[begin]+robWay(nums,begin+3);
int max=m1>m2?m1:m2;
return max;
}
}
运行结果:

方法2:动态规划
针对第n个房间,要么偷,要么不偷。
偷:take=noTake+nums[i];
不偷:noTake=maxProfile;
maxProfile=Math.max(take,noTake);
代码如下:
public class Solution {
public int rob(int[] nums) {
int take=0;
int noTake=0;
int maxProfit=0;
for(int i=0;i<nums.length;i++){
take=nums[i]+noTake;
noTake=maxProfit;
maxProfit=Math.max(take,noTake);
}
return maxProfit;
}
}
运行结果:

(easy)LeetCode 198.House Robber的更多相关文章
- leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)
House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...
- leetcode 198. House Robber (Easy)
https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如 ...
- [LeetCode] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Leetcode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java for LeetCode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java [Leetcode 198]House Robber
题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
- [LeetCode] 198. House Robber _Easy tag: Dynamic Programming
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Leetcode 198 House Robber 动态规划
题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...
- 【easy】198. House Robber 123总结……
题目一: 一个极其简单的动态规划. class Solution { public: int rob(vector<int>& nums) { ; // 表示没有选择当前house ...
随机推荐
- servlet & filter & listener & interceptor
web.xml 的加载顺序是:context- param -> listener -> filter -> servlet * Servlet 对URL生效,用户处理用户的URL请 ...
- NoSQL之基础篇
NoSQL(NoSQL = Not Only SQL ),泛指非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2. ...
- input绑定datapicker控件后input再绑定blur或者mouseout等问题
input绑定datapicker控件后input再绑定blur或者mouseout等问题 问题描述:今天在修改一个东西的时候需要给一个input输入域绑定blur事件,从而当它失去焦点后动态修改其中 ...
- 股票自用指标 boll 菜刀
BI:=(H+L+O+C)/; BOL:EMA(BI,N); UPPER:BOLL+N1*STD(CLOSE,N); LOWER:BOLL-N1*STD(CLOSE,N); MA1:MA(CLOSE, ...
- android学习笔记42——图形图像处理2——绘图
绘图 android的绘图应该继承View组件,并重写onDraw(Canvas canvas)方法即可. 重写onDraw(Canvas canvas)方法时涉及一个绘图API:Canvas,Can ...
- datagridview 不显示行号的问题
环境:C#,Winform 场景: 窗体上有两个tab页A.B,每个tab页上都有一个DatagridView.窗体加载后,显示tab A选项卡.序号正常显示,但点击B选项卡后,DatagridVie ...
- (三)java的数据类型
java是一种强类型的语言,所谓强类型,意味着每个变量都要有确定的类型,每个表达式也要有明确的类型,包括传递的某些参数.java中从大的方面分有两大数据类型,分别是基本数据类型和引用数据类型,基本数据 ...
- Excel 操作类
转载:http://www.cnblogs.com/fellowcheng/archive/2010/08/21/1805158.html ExcelHelper(Excel2007) Code hi ...
- python 读取sqlite3 数据库
import sqlite3 name = "tom" age = 30 con = sqlite3.connect("d:\\test.db") cur = ...
- hibernateUtils
hibernate3.x版本 package hibernate_01; import org.hibernate.Session; import org.hibernate.SessionFacto ...