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. WebAPI Post类型传参报错“找不到与该请求匹配的操作”

    错误内容: Message=未找到与请求 URI“http://localhost:42914/api/Products/Login”匹配的 HTTP 资源. MessageDetail=在控制器“P ...

  2. C# toString()转换详细(转)

    文章转自:http://blog.csdn.net/xiaoguang44/article/details/6988418 字符型转换为字符串 // C 货币 2.5.ToString("C ...

  3. 【Reporting Services 报表开发】— 表达式

    一.常用的SSRS原始函数可以打开文本框的表达式中看到,如图1 图1 如下为SSRS中设计报表时常用的运算函数: 运算符/函数 说明 + 前后位数字则为加法,前后为字符串则为链接符号 - 数值减法 * ...

  4. Ruby Class

    类定义: class 类名 类定义 end ※类名大写字母开始!!! 构造方法(initialize) 类名调用new方法的时候,触发的一个方法. def initialize(my_name = & ...

  5. CentOS6.8安装Redis3.2.5

    1.下载Redis3.2.5安装包           wget http://download.redis.io/releases/redis-3.2.5.tar.gz 2.解压.编译.安装redi ...

  6. bzoj3034: Heaven Cow与God Bull

    Description __int64 ago,there's a heaven cow called sjy...A god bull named wzc fell in love with her ...

  7. MFC的类层次结构图

  8. 关于List泛型的强制转换

    当我们从数据库中查询出一些数据,有时返回的结果可能是List<Object>类型,而我们清楚的知道它的准确类型是List<User>,可能我们想直接的去进行类型的转换,你可能会 ...

  9. BPEL_Oracle BPEL新一代工作流介绍(概念)

    2014-11-02 Created By BaoXinjian

  10. DirFile

    using System; using System.Text; using System.IO; namespace MyListen { /// <summary> /// 文件操作夹 ...