关闭

LeetCode-Easy部分中标签为Dynamic Programming *198. House Robber

标签: leetcode
834人阅读 评论(2) 收藏 举报
分类:

原题

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.

代码分析

这是一道很难理解的DP问题。我现在也没完全理解怎么想出这个题的解题思路,如何分析的。

        //nums = {1, 8, 12 10, 9 }  
        public int Rob(int[] nums)
        {
            int inCur = 0; //偷当前房子能获取的最大钱数
            int noInCur = 0;//不偷当前的房子能收获的最大钱数
            for (int i = 0; i < nums.Length; i++)
            {
                int tmp = nums[i]+ noInCur; //偷 ith 房子,上一个房子就不能偷
                noInCur = Math.Max(inCur, noInCur); //如果不偷 ith 房子,则noInCur 等于偷 i-1 房子和不偷 i-1 房子的最大值
                inCur = tmp; //偷当前房子后的获取总钱数
            }
            return Math.Max(inCur, noInCur);
        }

leetcode-solution库

leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp

3
0
查看评论

198. House Robber Leetcode Python

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 yo...
  • hyperbolechi
  • hyperbolechi
  • 2015-03-31 22:28
  • 1257

leetCode198-House Robber

链接:https://leetcode.com/problems/house-robber/ 这道理可以看做是状态压缩,每两个数字看做是一行,状态有3个,故需要F[N][3]的数组,F[i][j]就表示第i行状态j时rob的money。 具体状态压缩可以看我这两篇blog: 算法练习系列—hiho1...
  • Lu597203933
  • Lu597203933
  • 2015-04-01 18:28
  • 7394

Leetcode_198_House Robber

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47680663 You are a professional robber planning to rob houses along a stree...
  • pistolove
  • pistolove
  • 2015-08-15 12:02
  • 2099

198. House Robber [easy] (Python)

题目链接 https://leetcode.com/problems/house-robber/ 题目原文 You are a professional robber planning to rob houses along a street. Each house has a cert...
  • coder_orz
  • coder_orz
  • 2016-06-01 14:39
  • 1363

LeetCode_198. House Robber_动态规划

198. House Robber You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only co...
  • Binbin_Sun
  • Binbin_Sun
  • 2016-01-21 12:45
  • 434

Leetcode 198: House Robber

House Robber Total Accepted: 642 Total Submissions: 2562You are a professional robber planning to rob houses along a street. Each house has a certain...
  • xudli
  • xudli
  • 2015-04-01 02:51
  • 4436

动态规划专题之----198. House Robber

标签(空格分隔): LeetCode题目链接
  • yinchaoji_
  • yinchaoji_
  • 2017-04-13 01:26
  • 107

leetcode 198House Robber(简单动态规划解法)

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 yo...
  • lwb102063
  • lwb102063
  • 2016-11-03 15:53
  • 178

Leetcode:198. House Robber(JAVA)

【问题描述】 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint...
  • u010183658
  • u010183658
  • 2016-03-04 09:30
  • 252

Dynamic Programming:198. House Robber

这个题我没有用动态规划。我是这样想的: 假设每一家的钱nums是:5 1 7 3 2 4. 我定义一个新的数组vector tmp(nums.size()),令dp[i]的含义是,抢劫nums[i]这一家的话能得到的最大收益。那么这个题的答案就是这个数组里最大的那个数。我的代码: class ...
  • u012528000
  • u012528000
  • 2017-09-28 19:39
  • 33
    算法channel

    交流思想,注重分析,实例阐述,通俗易懂,包含但不限于:经典算法,机器学习,深度学习,LeetCode 题解,Kaggle 实战。期待您的到来!

    算法与人工智能交流群:646901659

    个人资料
    • 访问:319417次
    • 积分:7589
    • 等级:
    • 排名:第3380名
    • 原创:350篇
    • 转载:1篇
    • 译文:0篇
    • 评论:58条
    博客专栏