Minimum Sum Path In 3 | Javascript program for Min path sum of 3D - array ; A utility function that returns minimum of 3 integers ; function to calculate MIN path sum of 3D array ; Initialize first row of tSum array ; Initialize first column of tSum array ; Initialize first width of tSum array ; Initialize first row - First column of tSum array ; Initialize first row - First width of tSum array ; Initialize first width - First column of tSum array ; Construct rest of the tSum array ; Driver Code
var l = 3 ; var m = 3 ; var n = 3 ; function min ( x , y , z ) { return ( x < y ) ? ( ( x < z ) ? x : z ) : ( ( y < z ) ? y : z ) ; } function minPathSum ( arr ) { var i , j , k ; var tSum = Array ( l ) ; for ( var i = 0 ; i < l ; i ++ ) { tSum [ i ] = Array . from ( Array ( m ) , ( ) => Array ( n ) ) ; } tSum [ 0 ] [ 0 ] [ 0 ] = arr [ 0 ] [ 0 ] [ 0 ] ; for ( i = 1 ; i < l ; i ++ ) tSum [ i ] [ 0 ] [ 0 ] = tSum [ i - 1 ] [ 0 ] [ 0 ] + arr [ i ] [ 0 ] [ 0 ] ; for ( j = 1 ; j < m ; j ++ ) tSum [ 0 ] [ j ] [ 0 ] = tSum [ 0 ] [ j - 1 ] [ 0 ] + arr [ 0 ] [ j ] [ 0 ] ; for ( k = 1 ; k < n ; k ++ ) tSum [ 0 ] [ 0 ] [ k ] = tSum [ 0 ] [ 0 ] [ k - 1 ] + arr [ 0 ] [ 0 ] [ k ] ; for ( i = 1 ; i < l ; i ++ ) for ( j = 1 ; j < m ; j ++ ) tSum [ i ] [ j ] [ 0 ] = min ( tSum [ i - 1 ] [ j ] [ 0 ] , tSum [ i ] [ j - 1 ] [ 0 ] , 1000000000 ) + arr [ i ] [ j ] [ 0 ] ; for ( i = 1 ; i < l ; i ++ ) for ( k = 1 ; k < n ; k ++ ) tSum [ i ] [ 0 ] [ k ] = min ( tSum [ i - 1 ] [ 0 ] [ k ] , tSum [ i ] [ 0 ] [ k - 1 ] , 1000000000 ) + arr [ i ] [ 0 ] [ k ] ; for ( k = 1 ; k < n ; k ++ ) for ( j = 1 ; j < m ; j ++ ) tSum [ 0 ] [ j ] [ k ] = min ( tSum [ 0 ] [ j ] [ k - 1 ] , tSum [ 0 ] [ j - 1 ] [ k ] , 1000000000 ) + arr [ 0 ] [ j ] [ k ] ; for ( i = 1 ; i < l ; i ++ ) for ( j = 1 ; j < m ; j ++ ) for ( k = 1 ; k < n ; k ++ ) tSum [ i ] [ j ] [ k ] = min ( tSum [ i - 1 ] [ j ] [ k ] , tSum [ i ] [ j - 1 ] [ k ] , tSum [ i ] [ j ] [ k - 1 ] ) + arr [ i ] [ j ] [ k ] ; return tSum [ l - 1 ] [ m - 1 ] [ n - 1 ] ; } var arr = [ [ [ 1 , 2 , 4 ] , [ 3 , 4 , 5 ] , [ 5 , 2 , 1 ] ] , [ [ 4 , 8 , 3 ] , [ 5 , 2 , 1 ] , [ 3 , 4 , 2 ] ] , [ [ 2 , 4 , 1 ] , [ 3 , 1 , 4 ] , [ 6 , 3 , 8 ] ] ] ; document . write ( minPathSum ( arr ) ) ;