Advertisement
Iam_Sandeep

Find A Specific Pair In Matrix

Jun 15th, 2022
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. def findMaxValue(mat, n):
  2.     dp=[[0]*n for i in range(n)]
  3.     dp[0][0]=mat[0][0]
  4.     ans=float('-inf')
  5.     for i in range(1,n):
  6.         dp[i][0]=min(mat[i][0],dp[i-1][0])
  7.         dp[0][i]=min(mat[0][i],dp[0][i-1])
  8.  
  9.  
  10.     for i in range(1,n):
  11.         for j in range(1,n):
  12.             #dp[i][j]=min(dp[i][j-1],dp[i-1][j],mat[i][j])
  13.             dp[i][j]=min(dp[i-1][j],dp[i][j-1],mat[i][j])
  14.             ans=max(mat[i][j]-dp[i-1][j-1],ans)
  15.    # print(dp)
  16.     return ans
  17. '''
  18. In dp table we store the minimum value of submatrix 0,0 to i,j
  19. ans=max(mat[i][j]-dp[i-1][j-1],ans) for all i,j
  20. '''
  21.    
  22.        
  23.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement