Please help me find the mistake.
Optimal Game Strategy - Wrong Answer
your approach is greedy
consider
4 6 2 3 1
according to this a picks 4 first but then b picks 6 a picks 2 b picks 3
b wins
however you should use try both substrates and get the answer for which coin should be picked
int q1 = a[i] + recursivefun(a, i+1, n) n=last index, i = first index
int q2 = a[n] + recursivefun(a, i, n-1)
ans = max(q1, q2)
return ans
So, I tried changed the code aptly but it still doesn’t seem to work.
How do I include 2 players?
see what the second player wants it to return minimum of the leftover game so he returns the minimum
Works fine, but I am getting TLE in once case.
Please help resolve that too.
you have to use the concept of DP for that
way 1 is memoisation store the result for ever i and n in a 2d matrix and if that substrate is already computed return it
way 2 is a 2d dp formulae
for (int gap = 0; gap < n; ++gap) {
for (int i = 0, j = gap; j < n; ++i, ++j) {
// Here x is value of F(i+2, j), y is F(i+1, j-1) and
// z is F(i, j-2) in above recursive formula
int x = ((i + 2) <= j) ? table[i + 2][j] : 0;
int y = ((i + 1) <= (j - 1)) ? table[i + 1][j - 1] : 0;
int z = (i <= (j - 2)) ? table[i][j - 2] : 0;
table[i][j] = max(arr[i] + min(x, y), arr[j] + min(y, z));
}
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.