Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Given an n x n matrix mat[n][n] of integers, find the maximum value of mat(c, d) – mat(a, b) over all choices of indexes such that both c > a and d > b
- '''
- def findMaxValue(mat, n):
- dp=[[float('inf')]*n for i in range(n)]
- dp[0][0]=mat[0][0]#This is min for matrix 0,0 to (1,1)
- ans=float('-inf')
- #dp[i][j] stores minimum from 0,0 to i-1,j-1 rectangle
- for i in range(n):
- for j in range(n):
- if i>=1:
- dp[i][j]=min(dp[i][j],dp[i-1][j],mat[i][j])
- if j>=1:
- dp[i][j]=min(dp[i][j],dp[i][j-1],mat[i][j])
- if i>=1 and j>=1:
- ans=max(ans,mat[i][j]-dp[i-1][j-1])#minimum of (i-1,j-1)'s rectangle at dp[i][j] i.e. 0,0 to (i-1,j-1) is stored at dp[i][j]
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement