Easy-题目26:198. House Robber

原创 2016年05月30日 20:11:11

题目原文:
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问题。设dp[n]为从头开始偷到第n家获得的钱数,则转移方程如下:
dp[n]=max(dp[n-1],dp[n-2]+money[n])
因为如果不偷第n家,则获得的最大钱数与偷前n-1家获得的钱数相同,如果偷第n家,则第n-1家不能偷,获得的钱数就是前n-2家的最大收益加上第n家里有的钱。
而编码过程中因为dp[n]只与n-1和n-2两项有关系,所以只需要保留两个变量,而不需开一个长度为n的数组。
源码:(language:c)

int rob(int* nums, int numsSize) {
    if(numsSize==0)
        return 0;
    if(numsSize==1)
        return nums[0];
    else if(numsSize==2)
        return nums[0]>nums[1]?nums[0]:nums[1];
    else
    {
        int i;
        int dp1,dp2,dp;

        dp1=nums[0];
        dp2=nums[0]>nums[1]?nums[0]:nums[1];
        for(i=2;i<numsSize;i++)
        {
            dp=dp2>dp1+nums[i]?dp2:dp1+nums[i];
            dp1=dp2;
            dp2=dp;
        }
        return dp;
    }
}

成绩:
0ms,beats3.60%,众数0ms,96.40%
Cmershen的碎碎念:
本题应该是从第一题以来第一道用dp解决的题目。以后遇到dp的题要多注意,多考虑dp数组构造的方法以及转移方程的确立。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

198. House Robber [easy] (Python)

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

House Robber - LeetCode 198

题目描述: You are a professional robber planning to rob houses along a street. Each house has a certain...
  • bu_min
  • bu_min
  • 2015年04月24日 22:07
  • 285

[LeetCode]--198. House Robber

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

LeetCode198题:House Robber【java】

先看下题目: You are a professional robber planning to rob houses along a street. Each house has a cert...
  • sswmjoy
  • sswmjoy
  • 2015年04月15日 16:51
  • 412

LeetCode-198&213.House Robber

https://leetcode.com/problems/house-robber/ You are a professional robber planning to rob houses...

Leetcode198——House Robber

Leetcode198——House Robber

【LeetCode从零单排】No198.House Robber &&No91.Decode Ways&&139 word break(动态规划典型应用)

1.题目一道典型的Dynamic Programming的题目。You are a professional robber planning to rob houses along a street....

LeetCode笔记:198. House Robber

盗马贼如何盗马使利益最大化

LeetCode 198 House Robber(强盗盗窃最大值)(动态规划)(*)

翻译你是一个专业强盗,并计划沿街去盗窃每一个住户。每个房子都有一定量的现金,阻止你盗窃的唯一阻碍是相邻的两个房子之间有安全系统。一旦这两个房子同时被盗窃,系统就会自动联系警察。给定一系列非负整数代表每...
  • NoMasp
  • NoMasp
  • 2016年01月22日 11:09
  • 2151

198. House Robber(C++实现)

动态规划求解,设n-2座房子最多能抢到f(n-2),n-1座房子能最多抢到f(n-1),n座房子最多能抢f(n),最后一座房子里有nMoney, 则可以推导出他们之间的关系(分两种情况,第一种不抢最后...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Easy-题目26:198. House Robber
举报原因:
原因补充:

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