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. css_样式样式器的分类

    详情:http://www.w3school.com.cn/h.asp 1.标签样式器:此样式器仅对html页面中div标签有效果 div{ background-color: rosybrown; ...

  2. SharePoint 2010 最佳实践学习总结------第2章 SharePoint Windows PowerShell指南

    第2章 SharePoint Windows PowerShell指南 SharePoint 2010是SharePoint系列产品中第一个开始支持Windows PowerShell的产品,在以前的 ...

  3. javassist AOP

    对于AOP,这个概念,不用解释,主要用途很多,我这里主要是为了后续研究如何实现APM做准备.前面研究了动态代理实现AOP,考虑到性能的问题,改用javassist直接修改直接码实现! javassis ...

  4. [转]了解oracle自治事务

    http://blog.csdn.net/indexman/article/details/7799862 1.什么是Oracle自治事务 在官方文档中,是这样的定义的“Autonomous tran ...

  5. C#Random()函数详解

    随机数的使用很普遍,可用它随机显示图片,用它防止无聊的人在论坛灌水还可以用来加密信息等等.本文讨论如何在一段数字区间内随机生成若干个互不相同的随机数,比如在从1到20间随机生成6个互不相同的整数,并通 ...

  6. OC—设计模式-通知的使用

    通知 通知(广播) 可以一对多的发送通知(一个发送者 多个观察者) 特别注意:在发送者 发送通知的时候,必须有观察者 发送者,就是注册一个通知中心,以他为中心,发送消息 通过通知的名字,来判断是哪个通 ...

  7. PHP中使用CURL实现get和post请求(总结)

    一.什么是curl curl是利用url语法在命令行方式下工作的开源文件传输工具. 二.PHP curl函数

  8. PHP基础学习笔记(一)

    1.初步了解PHP+ php是一种运行在服务端的跨平台的脚本语言. + php语法: <?php echo "welcome!": ?> php像javascript语 ...

  9. 【转】双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计

    架构简介 前几天网友来信说帮忙实现这样一个架构:只有两台机器,需要实现其中一台死机之后另一台能接管这台机器的服务,并且在两台机器正常服务时,两台机器都能用上.于是设计了如下的架构.此架构主要是由kee ...

  10. IntelliJ IDEA安装AngularJS插件

    Settings→Plugins→Install JetBrains plugins... 输入an后选择AngularJS,然后点击Install进行安装 按照提示重启IDEA 下图为插件管理窗口, ...