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.

题目意思:

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




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];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值