Minimum number of flips required such that the last cell of matrix can be reached from any other cell | Function to calculate the minimum number of flips required ; Dimensions of mat [ ] [ ] ; Initialize answer ; Count all ' D ' s in the last row ; Count all ' R ' s in the last column ; Print let answer ; Given matrix ; Function call ; Print let answer
function countChanges ( mat ) { let n = mat . length ; let m = mat [ 0 ] . length ; let ans = 0 ; for ( let j = 0 ; j < m - 1 ; j ++ ) { if ( mat [ n - 1 ] [ j ] != ' ' ) ans ++ ; } for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( mat [ i ] [ m - 1 ] != ' ' ) ans ++ ; } return ans ; } let arr = [ [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] , [ ' ' , ' ' , ' ' , ' ' ] ] ; let cnt = countChanges ( arr ) ; document . write ( cnt ) ;