(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 ...
随机推荐
- The connection to adb is down, and a severe error has occured.
启动android模拟器时.有时会报The connection to adb is down, and a severe error has occured.的错误.在网友说在任务管理器上把所有ad ...
- android 插件化开发 开源项目列表
开源的插件化框架 Qihoo360/DroidPlugin CtripMobile/DynamicAPK mmin18/AndroidDynamicLoader singwhatiwanna/dyna ...
- 十个 MongoDB 使用要点
转自: 十个 MongoDB 使用要点 从 mongodb 阶段性技术总结 中抽取并整理了对大家有帮助的十个要点: 1.mongodb 表名和字段名统一用小写字母 mongodb 是默认区分 ...
- Jedis编程设计:连接池
Jedis作为redis的最佳客户端,它提供了连接池的特性,"连接池"在通常情况下可以有效的提高应用的通信能力,并且这是一种良好的设计模式.Jedis的连接池设计基于apa ...
- Python Beautiful Soup模块的安装
以安装Beautifulsoup4为例: 1.到网站上下载:http://www.crummy.com/software/BeautifulSoup/bs4/download/ 2.解压文件到C:\P ...
- C#:基于WMI查询USB设备
来源:http://blog.csdn.net/jhqin/article/details/6734673 /* ------------------------------------------- ...
- 启动Memcache,出现memcached: error while loading shared libraries: libevent-1.4.so.1: cannot open shared
1.有可能是装了多个 libevent而导致memcache无法识别哪一个,解决方法就是卸载掉一个libevent 2.只安装了一个libevent,但是也报这个错,解决方法 32位系统下:ln ...
- insmod module_param 模块参数
模块参数 引导模块时,可以向它传递参数.要使用模块参数加载模块,这样写: insmod module.ko [param1=value param2=value ...] 为了使用这些参数的值,要在模 ...
- Oracle11g空表导出方法
今天凌晨在客户现场进行一个Oracle11g的数据库迁移,习惯性的用了exp/imp,然后在新的数据库发现,空表根本没有exp出来,然后查资料,发现了如下信息:[ORACLE 11G在用EXPORT导 ...
- Tomcat远程调试catalina.sh的配置
#!/bin/sh # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license ...