Maximum sum path in a matrix from top | Function to find the maximum sum path in the grid ; Dimensions of grid [ ] [ ] ; Stores maximum sum at each cell sum [ i ] [ j ] from cell sum [ 0 ] [ 0 ] ; Iterate to compute the maximum sum path in the grid ; Update the maximum path sum ; Return the maximum sum ; Driver Code
function MaximumPath ( grid ) { let N = grid . length ; let M = grid [ 0 ] . length ; let sum = new Array ( N + 1 ) ; for ( var i = 0 ; i < sum . length ; i ++ ) { sum [ i ] = new Array ( 2 ) ; } for ( var i = 0 ; i < sum . length ; i ++ ) { for ( var j = 0 ; j < sum . length ; j ++ ) { sum [ i ] [ j ] = 0 ; } } for ( let i = 1 ; i <= N ; i ++ ) { for ( let j = 1 ; j <= M ; j ++ ) { sum [ i ] [ j ] = Math . max ( sum [ i - 1 ] [ j ] , sum [ i ] [ j - 1 ] ) + grid [ i - 1 ] [ j - 1 ] ; } } return sum [ N ] [ M ] ; } let grid = [ [ 1 , 2 ] , [ 3 , 5 ] ] ; document . write ( MaximumPath ( grid ) ) ;