Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Python3 implementation to find the minimum number of changes required such that every path from top left to the bottom right are palindromic paths ; Function to find the minimum number of the changes required for the every path to be palindromic ; count variable for maintaining total changes . ; left and right variables for keeping distance values from cell ( 0 , 0 ) and ( N - 1 , M - 1 ) respectively . ; Iterating over the matrix ; Finding minimum number of changes required . ; Minimum no . of changes will be the the minimum no . of different values and we will assume to make them equals to value with maximum frequency element ; Moving ahead with greater distance ; Driver Code ; Function Call
M = 3 NEW_LINE N = 3 NEW_LINE def minchanges ( mat ) : NEW_LINE INDENT count = 0 NEW_LINE left = 0 NEW_LINE right = N + M - 2 NEW_LINE while ( left < right ) : NEW_LINE INDENT mp = { } NEW_LINE totalsize = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( i + j == left ) : NEW_LINE INDENT mp [ mat [ i ] [ j ] ] = NEW_LINE mp . get ( mat [ i ] [ j ] , 0 ) + 1 NEW_LINE totalsize += 1 NEW_LINE DEDENT elif ( i + j == right ) : NEW_LINE INDENT mp [ mat [ i ] [ j ] ] = NEW_LINE mp . get ( mat [ i ] [ j ] , 0 ) + 1 NEW_LINE totalsize += 1 NEW_LINE DEDENT DEDENT DEDENT changes = 0 NEW_LINE for itr in mp : NEW_LINE INDENT changes = max ( changes , mp [ itr ] ) NEW_LINE DEDENT count += totalsize - changes NEW_LINE left += 1 NEW_LINE right -= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 4 , 1 ] , [ 2 , 5 , 3 ] , [ 1 , 3 , 1 ] ] NEW_LINE print ( minchanges ( mat ) ) NEW_LINE DEDENT