<p>You are given an integer <code>n</code>. You have an <code>n x n</code> binary grid <code>grid</code> with all values initially <code>1</code>&#39;s except for some indices given in the array <code>mines</code>. The <code>i<sup>th</sup></code> element of the array <code>mines</code> is defined as <code>mines[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> where <code>grid[x<sub>i</sub>][y<sub>i</sub>] == 0</code>.</p>

<p>Return <em>the order of the largest <strong>axis-aligned</strong> plus sign of </em>1<em>&#39;s contained in </em><code>grid</code>. If there is none, return <code>0</code>.</p>

<p>An <strong>axis-aligned plus sign</strong> of <code>1</code>&#39;s of order <code>k</code> has some center <code>grid[r][c] == 1</code> along with four arms of length <code>k - 1</code> going up, down, left, and right, and made of <code>1</code>&#39;s. Note that there could be <code>0</code>&#39;s or <code>1</code>&#39;s beyond the arms of the plus sign, only the relevant area of the plus sign is checked for <code>1</code>&#39;s.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/13/plus1-grid.jpg" style="width: 404px; height: 405px;" />
<pre>
<strong>Input:</strong> n = 5, mines = [[4,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> In the above grid, the largest plus sign can only be of order 2. One of them is shown.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/13/plus2-grid.jpg" style="width: 84px; height: 85px;" />
<pre>
<strong>Input:</strong> n = 1, mines = [[0,0]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no plus sign, so return 0.
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 500</code></li>
	<li><code>1 &lt;= mines.length &lt;= 5000</code></li>
	<li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt; n</code></li>
	<li>All the pairs <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    int N;
    int dp1[501][501];
    int dp2[501][501];
    int arr[501][501];
    int total_max;

    int rec1(int i ,int j)
    {
        if(i<0 || j<0 || i>=N || j>=N)
        return 0;

        if(dp1[i][j]!=-1) return dp1[i][j];

        int ans = 1e5;

        if(arr[i][j]==1)
        {
            ans = min(ans,rec1(i-1,j));
            //ans = min(ans,rec(i+1,j));
            ans = min(ans,rec1(i,j-1));
            //ans = min(ans,rec(i,j+1));

            ans = ans + 1;
        }
        else
        ans =0;
        //cout << i << " " << j << " " << ans;
        //total_max = max(total_max,ans);
        return dp1[i][j] = ans;
    }

    int rec2(int i ,int j)
    {
        if(i<0 || j<0 || i>=N || j>=N)
        return 0;

        if(dp2[i][j]!=-1) return dp2[i][j];

        int ans = 1e5;

        if(arr[i][j]==1)
        {
            //ans = min(ans,rec(i-1,j));
            ans = min(ans,rec2(i+1,j));
            //ans = min(ans,rec(i,j-1));
            ans = min(ans,rec2(i,j+1));

            ans = ans + 1;
        }
        else
        ans =0;
        cout << i << " " << j << " " << ans;
        return dp2[i][j] = ans;
    }

    int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {
        N =n;
        memset(dp1,-1,sizeof(dp1));
        memset(dp2,-1,sizeof(dp2));
        total_max =0;
        int u[n][n],d[n][n],l[n][n],r[n][n];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                arr[i][j] = 1;
            }
        }
        for(auto v:mines)
        {
            arr[v[0]][v[1]] = 0;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(j==0)
                l[i][j] = arr[i][j];
                else
                {
                if(arr[i][j]==1)
                l[i][j] = arr[i][j] + l[i][j-1];
                else
                l[i][j] = 0;
                }

                if(i==0)
                u[i][j] = arr[i][j];
                else
                {
                if(arr[i][j]==1)
                u[i][j] = arr[i][j] + u[i-1][j];
                else
                u[i][j] = 0;
                }
            }
        }
        for(int i=n-1;i>=0;i--)
        {
            for(int j=n-1;j>=0;j--)
            {
                if(j==n-1)
                r[i][j] = arr[i][j];
                else
                {
                if(arr[i][j]==1)
                r[i][j] = arr[i][j] + r[i][j+1];
                else
                r[i][j] = 0;
                }

                if(i==n-1)
                d[i][j] = arr[i][j];
                else
                {
                if(arr[i][j]==1)
                d[i][j] = arr[i][j] + d[i+1][j];
                else
                d[i][j] = 0;
                }
            }
        }
        //rec1(n-1,n-1);
        //rec2(0,0);

        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                int an = min({l[i][j],r[i][j],u[i][j],d[i][j]});
                total_max = max(total_max,an);
            }
        }
        return total_max;
    }
};
28841