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. Show a heart shaped

    Windows Form application version: private void Form1_Load(object sender, EventArgs e)        {       ...

  2. 一个完整的JENKINS下的ANT BUILD.XML文件(Jenkins可以参考)

    一个完整的JENKINS下的ANT BUILD.XML文件 <?xml version="1.0" encoding="UTF-8"?> <p ...

  3. [转]开源那些事儿(四)-如何使用CodePlex进行项目管理

    本文版权信息作者:Jake Lin(Jake's Blog on 博客园) 出处:http://www.cnblogs.com/procoder/archive/2010/02/10/About-Op ...

  4. 使用Cyclone IV控制DDR2

    根据你的DDR2手册配置好megacore,megacore会生成一个example top: 在quartus中运行megacore生成的xxx_pin_assignments.tcl,指定DDR2 ...

  5. Configure custom SSL certificate for RDP on Windows Server 2012 in Remote Administration mode

    Q: So the release of Windows Server 2012 has removed a lot of the old Remote Desktop related configu ...

  6. Session和Cookie深度剖析

    Session和Cookie的区别 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案.同时我们也看到,由于采用服务器端保持状态的方案在客户端 ...

  7. 怎样进行Android UI元素设计

    Android UI元素里面包含了许多的内容,比如:该平台由操作系统.中间件.用户界面和应用软件组成,一个应用程序要想受用户喜爱,那么UI可不能差. Android为相似的编程名词引入了一些新的术语, ...

  8. Dell vsotro 14 3000系列从win10重装win7

    1. F2启动进入新的BIOS界面,首先Disable Secure Boot,然后把UEFI改为Legeacy模式,当然是改不回来的,不知道为什么: 2. 插入U盘(老毛桃+UEFI启动镜像): 3 ...

  9. JavaScript-CheckBox全选/反选

    //------------------------------------ // 全/反选 // param checkName checkbox的name属性 //---------------- ...

  10. spring2.5整合hibernate3.0整合Struts

    首先:这是spring framework+hibernate+struts集成,spring主要用于aop和ioc,hibernate主要是用于持久层,struts主要是用于mvc. 同时关于spr ...