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. PO、VO、BO、DTO

    J2EE开发中大量的专业缩略语很是让人迷惑,尤其是跟一些高手讨论问题的时候,三分钟就被人家满口的专业术语喷晕了,PO VO BO DTO POJO DAO,一大堆的就来了(听过老罗对这种现象的批判的朋 ...

  2. android 各国语言对应的缩写

    android资源文件夹的写法规则: 语言缩写-国家地区缩写 语言缩写 藏语:bo_CN en 英文 en_US 英文 (美国) ar 阿拉伯文 ar_AE 阿拉伯文 (阿拉伯联合酋长国) ar_BH ...

  3. HTML5常识总结(一)

    一.HTML5的发展历程 + html演变的几个版本: html2.0.html3.2.html4.0.html4.01.html5. + 其中在html4.01发布之后,还发布了xtml1.0,它是 ...

  4. SPOJ #752. Power it!

    By property of mod operations , we can simply use Divide and Conquer + Recursion to solve it. Refere ...

  5. python asyncio笔记

    1.什么是coroutine coroutine,最早我是在lua里面看到的,coroutine最大的好处是可以保存堆栈,让程序得以继续执行,在python里面,一般是利用yield来实现,具体可以看 ...

  6. SqlServer——阻止保存要求重新创建表的更改

    场景: 修改已有数据的列宽时,提示“阻止保存要求重新创建表的更改”. 解决: 工具-〉选项-〉左侧有个 设计器-〉表设计器和数据库设计器 -> 阻止保存要求重新创建表的更改(右侧) 把钩去掉即可 ...

  7. Env:Cscope安装与配置

    1. 介绍 Cscope是类似于ctags一样的工具,但可认为他是ctags的增强版. 2. 安装 sudo apt-get install cscope 通过源码安装,参照http://blog.c ...

  8. Android:单元测试Junit的配置

    在实际开发中,开发android软件的过程需要不断地进行测试.而使用Junit测试框架,侧是正规Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性.... ...

  9. WebAPI 安全性 使用TOKEN+签名验证(下)

    //根据请求类型拼接参数 NameValueCollection form = HttpContext.Current.Request.QueryString; string data = strin ...

  10. Form_Form Builder国际化多语言开发(案例)

    2014-05-06 Created By BaoXinjian