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
Problem Solving Ideas:
DP problem Dp[i]=nums[i]+math.max (DP[I-2], dp[i-3])
The Java implementation is as follows:
public int Rob (int[] nums) { if (nums==null| | nums.length==0) return 0; if (nums.length<=2) return Math.max (Nums[0], nums[nums.length-1]); Int[] Dp=new int[nums.length]; Dp[0]=nums[0]; DP[1]=NUMS[1]; DP[2]=NUMS[0]+NUMS[2]; for (int i=3;i<nums.length;i++) Dp[i]=nums[i]+math.max (Dp[i-2], dp[i-3]); Return Math.max (Dp[nums.length-1], dp[nums.length-2]); }
Java for Leetcode 198 house robber