Such problems as dynamic programming problem, so in the end the main thing is to write dynamic transfer equations
First, the array is assumed [2,7,9,3,1], when I and III masseur reservation (i.e., a number 9),
So I need to (9 + 2) compare and (7), it is possible to obtain equation
Math.max(dp[i-1],dp[i-2]+nums[i]) //nums[i]即为当前的按摩师
code show as below
public int massage(int[] nums) {
int n = nums.length;
if(n == 0)return 0;
if(n == 1)return nums[0];
int[] dp = new int[n];
dp[0] = nums[0];
dp[1] = Math.max(nums[0],nums[1]);
for(int i=2; i<n; i++){
dp[i] = Math.max(dp[i-1],dp[i-2]+nums[i]);
}
return dp[n-1];
}
This issue and force the issue the same series buckle robbery
If you are interested you can go hand rub
This category is only used as a personal record, Gangster light spray.