Iam_Sandeep

Optimal Strategy For a Game DP

Apr 28th, 2022
1,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. class Solution:
  2.     def optimalStrategyOfGame(self,A, n):
  3.         '''
  4.        Note if we use 3d array n*n*2 and apply DP its taking TLE so it is expected
  5.        to use n*n
  6.        '''
  7.         dp=[[-1 for i in range(1+n)] for j in range(n+1)]
  8.         def help(i,j):
  9.             if i>j:return 0
  10.             if dp[i][j]!=-1:return dp[i][j]
  11.             first=A[i]+min(help(i+2,j),help(i+1,j-1))
  12.             second=A[j]+min(help(i,j-2),help(i+1,j-1))
  13.             dp[i][j]=max(first,second)
  14.             return dp[i][j]
  15.         return help(0,n-1)
Advertisement
Add Comment
Please, Sign In to add comment