restart.'s blog

By restart., history, 9 years ago, In English

Suppose an array A[4]={3,2,10,4} is given. 2 player can pick a number from the front end of the array or last end of the array. if both player wants to make maximum score, what the maximum score first player can make?? in this case first player can make 13 {3,10}. obviously first player starts the picking. I tried this using recursion, but it does not give me correct ans. it gives 17. please help me which states i am missing... I am new to recursion.. TIA..

//here is my code
//initially value=0,left=0,right=n-1=3
int rec(int value,int left,int right)
{
    if(left>right or left>=n or right<0) return 0;
    int p1=value+rec(A[left],left+1,right);
    int p2=value+rec(A[right],left,right-1);
    int mx=max(p1,p2 );
    return mx;
}
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
9 years ago, # |
Rev. 2   Vote: I like it +1 Vote: I do not like it

Denote ans(l, r) the maximum sum the player who moves this turn can accumulate. Then ans(l, r) = s(l, r) - min(ans(l + 1, r), ans(l, r - 1)), where s(l, r) is equal the the sum of elements from index l to r.

»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I can also solve this problem using DP.If you need code here is my code http://paste.ubuntu.com/12721489/