Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def optimalStrategyOfGame(self,A, n):
- '''
- Note if we use 3d array n*n*2 and apply DP its taking TLE so it is expected
- to use n*n
- '''
- dp=[[-1 for i in range(1+n)] for j in range(n+1)]
- def help(i,j):
- if i>j:return 0
- if dp[i][j]!=-1:return dp[i][j]
- first=A[i]+min(help(i+2,j),help(i+1,j-1))
- second=A[j]+min(help(i,j-2),help(i+1,j-1))
- dp[i][j]=max(first,second)
- return dp[i][j]
- return help(0,n-1)
Advertisement
Add Comment
Please, Sign In to add comment