How does Java solve the problem of robbing houses?
This article mainly explains "how to solve the problem of home robbery by Java". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Next, let's let the editor take you to learn "how to solve the problem of robbery by Java".
If you want to rob the nth house, you can't rob the nth house, so the money you get from robbing the nth house is the money of the nth family plus the most money obtained by the former Nmai 2 families, that is, f (nmur2) + nums (n). If you don't rob the nth house, the maximum profit is f (nMuth1). We have to go to the larger one, so the dynamic transfer equation is:
F (n) = max (nums [n] + f (nmur2), f (nmur1))
Package mainimport "fmt" func max (return b int) int {if a > b {return a} return b} func rob (nums [] int) int {if len (nums) = 0 {return 0} if len (nums) = 1 {return nums [0]} dp: = make ([] int,len (nums) dp [0] = nums [0] dp [1] = max (nums [0]) Nums [1]) maxVal: = dp [1] for iRu 2 I