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

原创 2017年04月10日 14:23:13

原题

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

版权声明:本文为博主原创文章,欢迎转载,请注明 http://blog.csdn.net/daigualu

[Leetcode刷题总结系列][Dynamic programming]198. House Robber

Leetcode 198. House Robber You are a professional robber who is planing to rob houses along a street...

198. House Robber [easy] (Python)

题目链接 https://leetcode.com/problems/house-robber/ 题目原文 You are a professional robber planning t...

LeetCode#198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount...

leetcode 198 & 213:House Robber

一.House Robber I 的题意是:有一排商铺,强盗不能连续抢劫相邻的两家,否则会触动报警器,求强盗在不触动报警器的前提下,可以抢劫的最多金钱总和。(输入一维数组,从中选择若干个数,使得这些数...

LeetCode动态规划198. House Robber思路解析

LeetCode动态规划198. House Robber思路解析,用最容易理解的递归解释

leetcode 198 House Robber

今天看了一个华为西安研究院的一个女生代码大神的总结很有感悟,下面这句话送给大家:只有好的程序员才能写出人类可以理解的代码You are a professional robber planning t...

Leetcode 198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount...

Leetcode[198]-House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount...

Leetcode_198_House Robber

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47680663 You are a profes...

LeetCode - 198. House Robber

每一个house都有rob和not rob两种状态,所以我们可以使用一个[n][2]的矩阵来记录每个房子被抢和不被抢的情况下所累计的钱的数量,这道动态规划问题的四要素如下: state: f[i][...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode-Easy部分中标签为Dynamic Programming *198. House Robber
举报原因:
原因补充:

(最多只允许输入30个字)