<p>Given an <code>n x n</code> array of integers <code>matrix</code>, return <em>the <strong>minimum sum</strong> of any <strong>falling path</strong> through</em> <code>matrix</code>.</p>

<p>A <strong>falling path</strong> starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position <code>(row, col)</code> will be <code>(row + 1, col - 1)</code>, <code>(row + 1, col)</code>, or <code>(row + 1, col + 1)</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/03/failing1-grid.jpg" style="width: 499px; height: 500px;" />
<pre>
<strong>Input:</strong> matrix = [[2,1,3],[6,5,4],[7,8,9]]
<strong>Output:</strong> 13
<strong>Explanation:</strong> There are two falling paths with a minimum sum as shown.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/03/failing2-grid.jpg" style="width: 164px; height: 365px;" />
<pre>
<strong>Input:</strong> matrix = [[-19,57],[-40,-5]]
<strong>Output:</strong> -59
<strong>Explanation:</strong> The falling path with a minimum sum is shown.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == matrix.length == matrix[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code>-100 &lt;= matrix[i][j] &lt;= 100</code></li>
</ul>

class Solution {
public:
int mini(int i,int j,vector<vector<int>> triangle,int n,vector<vector<int>> &dp){
      

   if(j<0 || j>=n) return 1e9;
    if (i == 0 )
        return triangle[0][j];

    
    if (dp[i][j] != -1)
        return dp[i][j];
   
    int down = triangle[i][j] + mini(i - 1, j, triangle, n, dp);
    int up =   triangle[i][j] + mini(i-1,j-1,triangle,n,dp);
    int diagonal = triangle[i][j] + mini(i - 1, j + 1, triangle, n, dp);
   return dp[i][j] = min(down , min(diagonal,up));}


    int minFallingPathSum(vector<vector<int>>& matrix) {
        int n  = matrix.size();
        int m = matrix[0].size();
vector<vector<int>> dp(n,vector<int> (m,0));


        for(int i=0;i<n;i++){
            dp[0][i] = matrix[0][i];
        }
        for(int i =1;i<n;i++){
            for(int j =0;j<m;j++){
                   // Large number for comparison

                // Path coming straight down from the row above
                int up = matrix[i][j] + dp[i-1][j];

                // Path coming from the left diagonal
                int leftDiagonal = matrix[i][j];
                if (j-1 >= 0) {
                    leftDiagonal += dp[i-1][j-1];
                }
                else{
                    leftDiagonal += 1e9;
                }

                // Path coming from the right diagonal
                int rightDiagonal = matrix[i][j];
                if (j < m - 1) {
                    rightDiagonal += dp[i-1][j+1];
                }
                else{
                    rightDiagonal = 1e9;
                }

                // Find the minimum of the three possible paths
                
                // Store the result in the dp array
                dp[i][j] =  min(up, min(leftDiagonal, rightDiagonal));

            }
            
        }
int minii = 1e9;
        for(int i = 0;i<m;i++){
            minii = min(minii,dp[n-1][i]);
        }
        return minii;
    }
        
};
13700