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的更多相关文章

  1. 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:二叉树下的不能相邻,求能 ...

  2. leetcode 198. House Robber (Easy)

    https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如 ...

  3. [LeetCode] 198. House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  4. Leetcode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  5. Java for LeetCode 198 House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  6. Java [Leetcode 198]House Robber

    题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...

  7. [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 ...

  8. Leetcode 198 House Robber 动态规划

    题意是强盗能隔个马抢马,看如何获得的价值最高 动态规划题需要考虑状态,阶段,还有状态转移,这个可以参考<动态规划经典教程>,网上有的下的,里面有大量的经典题目讲解 dp[i]表示到第i匹马 ...

  9. 【easy】198. House Robber 123总结……

    题目一: 一个极其简单的动态规划. class Solution { public: int rob(vector<int>& nums) { ; // 表示没有选择当前house ...

随机推荐

  1. C#-提取网页中的超链接

    转载:http://www.wzsky.net/html/Program/net/26849.htmlusing System; using System.Xml; using System.Text ...

  2. openssl数字证书私钥删除私钥密码

    解密 openssl rsa -in server.key.org -out server.key

  3. Innodb 表修复(转)

    摘要:      突然收到MySQL报警,从库的数据库挂了,一直在不停的重启,打开错误日志,发现有张表坏了.innodb表损坏不能通过repair table 等修复myisam的命令操作.现在记录下 ...

  4. TP框架基础

    什么是TP框架: 一堆代码的集合,里边有变量.函数.类.常量,设计模式MVC.AR数据库.单例等等.全称是Tinkphp框架; 为什么使用框架: 使用框架将全部精力集中在业务层次,节省50-60%的工 ...

  5. WCF学习心得----(四)服务承载

    WCF学习心得----(四)服务承载 这一章节花费了好长的时间才整理个大概,主要原因是初次接触这个东西,在做练习实践的过程中,遇到了很多的问题,有些问题到目前还没有得以解决.所以在这一章节中,有一个承 ...

  6. .NET,Cookie,写Cookie,取Cookie

    Cookie是一段文本信息,在客户端存储 Cookie 是 ASP.NET 的会话状态将请求与会话关联的方法之一.Cookie 也可以直接用于在请求之间保持数据,但数据随后将存储在客户端并随每个请求一 ...

  7. bzoj3521: [Poi2014]Salad Bar

    Description 有一个长度为n的字符串,每一位只会是p或j.你需要取出一个子串S(从左到右或从右到左一个一个取出),使得不管是从左往右还是从右往左取,都保证每时每刻已取出的p的个数不小于j的个 ...

  8. android学习笔记34——ClipDrawable资源

    ClipDrawable ClipDrawable代表从其他位图上截取一个“图片片段” 在XML文件中定义ClipDrawable对象使用<clip.../>元素,该元素的语法为: 以上语 ...

  9. TCP/IP四层模型和OSI七层模型的概念

    转:http://blog.csdn.net/superjunjin/article/details/7841099/ TCP/IP四层模型 TCP/IP是一组协议的代名词,它还包括许多协议,组成了T ...

  10. [platform]linux platform device/driver(二)--Platform Device和Platform_driver注册过程之详细代码

    转自:http://www.cnblogs.com/haimeng2010/p/3582403.html 目录: 1.platform_device注册过程 2.platform_driver注册过程 ...