3
int f(int i, int j, vector<vector<int>> &matrix, vector<vector<int>> &dp) {4
// to handle edge cases if the j index is less than 0 or greater than size6
if (j < 0 or j >= matrix[0].size()) return 1e8;9
if (i == 0) return dp[0][j] = matrix[0][j];11
if (dp[i][j] != -1) return dp[i][j];15
// Here we have to iterate through all the possiblities in the entire upper16
// row for each element except for the element which is directly above the17
// current element in the matrix, and then we have to find the minimum.18
for (int k = 0; k < matrix[0].size(); k++) {20
ans = min(ans, matrix[i][j] + f(i - 1, k, matrix, dp));23
return dp[i][j] = ans;26
int minFallingPathSum(vector<vector<int>> &grid) {30
vector<vector<int>> dp(n, vector<int>(n, -1));32
for (int j = 0; j < n; j++) {33
mini = min(mini, f(n - 1, j, grid, dp));