题目:
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
题目大意:给定一个数组,求不相邻元素的最大和。比如 {3,2,4,7} 最大和为10.解题思路:用动态规划来解。用一组数组来代表当前元素前的不相邻元素最大和;
求和公式为 dp[i] = Math.max( nums[i] + dp[ i-2 ] ,dp[ i-1 ] );
另解:用奇偶数来求最大和