# (DP) House robber

Source: Internet
Author: User

Topic:

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.

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.

Main topic:

You are a professional thief, to steal all over the street, this street every household inside are rich, but you can not steal the adjacent two, or will trigger an alarm, how to steal to ensure maximum benefit.

That is to give a one-dimensional array, the stored is a positive number, the maximum sum of non-contiguous.

Solution:

This is a one-dimensional simple DP problem, similar to array continuous maximum and.

Set Len to an array of lengths, nums arrays store each deposit, and the re array stores the maximum non-contiguous sum of the corresponding length array, i.e. the value of re[i], when the array length is I the discontinuous maximum sum.

Such as:

Index 0 1 2 3 4

Nums 5 2 4 7 1
Re 5 5 9 12 12

Len 1 2 3 4 5

State transitions:

Assuming that re[n] is required, because the adjacent values cannot be taken, then re[n-2] must be considered, so Re[n]=max (Re[n-1], nums[n-1]+re[n-2])

Initial state:

Len=1, Re[1]=nums[0]

len=2, Re[2]=max (re[1], nums[1]+0)

Len=3, Re[3]=max (re[2], nums[2]+re[1])

...

The current Len=n from above can only be obtained by the re[n-1 of the RE array, re[n-2], so only the first two states are saved with two values.

Java code:

` Public classSolution { Public intRobint[] nums) {        if(Nums = =NULL|| Nums.length = = 0)return0; intA = 0, b = 0, TMP;  for(inti = 0; i<nums.length; i++) {tmp=b; b= Nums[i] + a > B? Nums[i] +a:b; A=tmp; }        returnb; }}`

Python code:

`class Solution:     # @param {integer[]} nums    # @return {integer}    def Rob (Self, nums):         = B = 0        for in  xrange (len (nums)):            = b, max (Nums[i] + A, b) c19/>return b`

(DP) House robber

Related Keywords:

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.