Maximum sum path in a matrix from top | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaximumPath ( vector < vector < int > > & grid ) { int N = grid . size ( ) ; int M = grid [ 0 ] . size ( ) ; vector < vector < int > > sum ; sum . resize ( N + 1 , vector < int > ( M + 1 ) ) ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { sum [ i ] [ j ] = max ( sum [ i - 1 ] [ j ] , sum [ i ] [ j - 1 ] ) + grid [ i - 1 ] [ j - 1 ] ; } } return sum [ N ] [ M ] ; } int main ( ) { vector < vector < int > > grid = { { 1 , 2 } , { 3 , 5 } } ; cout << MaximumPath ( grid ) ; return 0 ; }