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 bro Keninto 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.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Also thanks to @ts for adding additional test cases.
Subscribe to see which companies asked this question
1 Public classSolution {2 Public intRobint[] nums) {3 if(NULL= = Nums | | Nums.length = = 0)return0;4 if(Nums.length = = 1)returnNums[0];5 //if (nums.length = = 2) return nums[0] > nums[1]? nums[0]: nums[1];6 int[] res =New int[nums.length];7 8Res[0] = nums[0];9Res[0] = Math.max (Nums[0], nums[1]);Ten for(inti = 2;i < res.length; i++){ OneRes[i] = Math.max (res[i-2]+nums[i],res[i-1]); A } - returnRes[res.length-1]; - } the}
198 House Robber Java Solutions