Optimal game strategy-2


why does this code not work…plz correct it along with explaination…
how to decide the size of dp array to be taken…plz explain

Hello @S19LPPP0202,

  1. To debug your code you can refer to the following source:
    https://www.geeksforgeeks.org/optimal-strategy-for-a-game-set-2/

  2. The size is decided by the table itself.
    For 2D DP: the number of rows depends upon what does the row signifies or indicates
    and similarly the size of column is defined by what does the column signify:

Suppose, for some arbitrary question, you have to create a table where the row defines the number of person and column defines the sum.
So, the size of rows will be maximum number possible of a person and column would be the maximum sum possible.

Hope, this would help.

how to decide upon the base case i have reffered to geeks for geeks first…still having doubt …
dont give geeks for geeks link

Sure @S19LPPP0202,

I won’t give the next time. But, they have explained it well, so i shared.

Now, talking about the base case:
As, you know, you are just computing the score of Piyush and Piyush started picking the coins first. Also, the value of n is even.

So, the simplest form of this question i.e. when there is only 2 coins and Piyush has to choose from them. Then, he will choose the maximum valued coin and hence, forming the base case of our solution.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.