Start with the last guest
- Accept the appointment, then f(n) = f(n-2) + value(n);
- No appointment is accepted, then f(n) = f(n-1);
- Take the larger of 1 and 2 as the maximum value of max
/***************************/
/* 按摩师问题
按摩师不能连续接单,至少需要间隔一位客人
计算出按摩师最多能接单多少
/***************************/
#include <stdio.h>
#include <algorithm>
int yuyue[] = {2,1,4,5,3,1,1,3};
int message(int *num, int numsize) {
int dp[numsize +1];
dp[1] = num[0];
dp[2] = std::max(num[0],num[1]);
for (int i=3; i<numsize+1; ++i) {
int x = num[i-1] + dp[i-2];
int y = dp[i-1];
dp[i] = std::max(x, y);
}
return dp[numsize];
}
int main() {
printf("best value = [%d]", message(yuyue, sizeof(yuyue)));
}