Leetcode 198 House Robber robbery maximum amount

Source: Internet
Author: User
Original title Address

https://leetcode.com/problems/house-robber/ Topic Description

You is a professional robber planning to rob houses along a street. Each house have a certain amount of money stashed, the only constraint stopping all from robbing each of the them are that Adjac ENT houses have security system connected and it would automatically contact the police if the adjacent houses were broken Into on the same night.
You are a professional robber, planning to rob a street house. Every house has a certain amount of money, the only limit you rob all the money in the house is: the adjacent House security system is connected together, if the adjacent two houses were stolen at the same night, will automatically alarm.

Given a list of non-negative integers representing the amount of money in each house, determine the maximum amount of mone Y you can rob tonight without alerting the police.
Give a series of non-negative integers representing the amount of money in each house, and calculate the maximum money you can grab without triggering an alarm tonight.

Tags Dynamic Programming Problem Solving thinking

The key question for solving this problem is whether we rob or rob a house. Suppose we use rob[i] to represent the maximum amount of money that can be obtained in all subsequent houses in the case of robbing the House of room I, using notrob[i] to indicate the maximum amount that can be obtained in all subsequent houses without robbing the house of the first I. In solving the rob[i], rob the house of the first I, obviously cannot continue to rob its next house , therefore rob[i] need according to notrob[i+1] to calculate, for Notrob[i], we do not rob the house of the first room, the first i+ 1 houses can either be robbed or not robbed , we calculate notrob[i according to the larger of the rob[i+1] and notrob[i+1].

Rob[i] : In the case of robbing the House of the first I, the money can be obtained from all the houses behind it (Nums[i]~nums[n]).

Notrob[i] : The money that can be obtained from all the houses behind it (Nums[i]~nums[n]) without robbing the house of the first room.

The equation is calculated as follows:

ROB[I]=NUM[I]+NOTROB[I+1] rob[i] = Num[i] + notrob[i + 1]
If you rob the first room, you must not rob the back of the neighboring house.

Notrob[i]=bigger (rob[i+1],notrob[i+1]) notrob[i] = bigger (rob[i + 1], Notrob[i + 1])
If there is no robbery, I can rob or not rob the neighboring house. The algorithm initializes the last day of the rob[n]=num[n] and notrob[n]=0 from the back forward, rob[i] = Num[i] + notrob[i + 1],notrob[i] = bigger (rob[i + 1], Notrob [i + 1]) Return to bigger (Rob[0], notrob[0]); Code

/** * Calculates the maximum amount of possible robbery * @param nums amount in each house * @param numssize number of houses * @return The maximum amount of robbery */int Rob (int* nums, int numssize)

    {int bigger (int, int);

    if (numssize = = 0) return 0;
    When robbing a house, the maximum amount that can be robbed from behind all the houses is int *maxwhenrob = (int *) malloc (sizeof (int) * numssize);

    Without robbing a house, the maximum amount of money that can be robbed from behind all the houses is int *maxwhennotrob = (int *) malloc (sizeof (int) * numssize);
    int index = numsSize-1;
    /* Initialize data for the last day */* (Maxwhenrob + index) = * (Nums + index);
    * (Maxwhennotrob + index) = 0;  /* Cycle before the House */for (--INDEX; index >-1;--index) {//Rob a house must not rob the back of the neighboring house * (Maxwhenrob + index) =
        * (Nums + index) + * (Maxwhennotrob + index + 1); Do not rob a house, the back of the house can Rob also can not Rob, choose the larger of the * (Maxwhennotrob + index) = bigger (* (Maxwhenrob + index + 1), * (M
    Axwhennotrob + index + 1));
    } int max = bigger (*maxwhenrob, *maxwhennotrob);
    Free (maxwhenrob);
    Free (maxwhennotrob);
return Max; }/** returns the larger of the two */int bigger (intA, int b) {return a > b a:b;} 

Complete code HTTPS://GITHUB.COM/ORANGE1991/LEETCODE/BLOB/MASTER/198/CPP/MAIN.C test Data

[4,3,5,6,1,2,0,4,2,5]: 21

2015/8/5

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.