LeetCode题-coins in a line

There are n coins in a line. Two players take turns to take acoin from one of the ends of the line until there are no more coinsleft. The player with the larger amount of money wins. Assume thatyou go first, describe an algorithm to compute the maximum amountof money you can win.

题目意思:

一排硬币,面值不同,然后从一排硬币的两头选择,两个人来选择,谁会能得到最大面值的硬币。

LeetCode题-coins in a line



int maxMoney(int A[],int N){
int P[MAX_N][MAX_N]={0};
int a,b,c;
for(int i=0;i<N;i++){
for(int m=0,n=i;n<N;m++,n++){
assert(m<N);assert(n<N);
a=((m+2<=N-1)?P[m+2][n]:0);
b=((m+1<=N-1&&n-1>=0)?P[m+1][n-1]:0);
c=(n-2>=0)?P[m][n-2]:0;
P[m][n]=max(A[m]+min(a,b),A[n]+min(b,c));
}
}
return P[0][N-1];
}