<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 PreComp {
   public:
    int l, r, u, d;
};

class Solution {
   private:
    void fillPfSf(vector<vector<PreComp>>& preCalc,
                  vector<vector<int>>& grid) {
        int n = grid.size();
        // fill row
        for (int row = 0; row < n; row++) {
            for (int col = 0; col < n; col++) {
                if (col == 0)
                    preCalc[row][col].l = grid[row][col], preCalc[row][n - col - 1].r = grid[row][n - col - 1];
                else {
                    if (grid[row][col] == 0)
                        preCalc[row][col].l = 0;
                    else
                        preCalc[row][col].l = 1 + preCalc[row][col - 1].l;

                    if (grid[row][n - col - 1] == 0)
                        preCalc[row][n - col - 1].r = 0;
                    else
                        preCalc[row][n - col - 1].r = 1 + preCalc[row][n - col].r;
                }
            }
        }
        // fill col
        for (int col = 0; col < n; col++) {
            for (int row = 0; row < n; row++) {
                if (row == 0)
                    preCalc[row][col].u = grid[row][col], preCalc[n - row - 1][col].d = grid[n - row - 1][col];
                else {
                    if (grid[row][col] == 0)
                        preCalc[row][col].u = 0;
                    else
                        preCalc[row][col].u = 1 + preCalc[row - 1][col].u;

                    if (grid[n - row - 1][col] == 0)
                        preCalc[n - row - 1][col].d = 0;
                    else
                        preCalc[n - row - 1][col].d = 1 + preCalc[n - row][col].d;
                }
            }
        }
    }

   public:
    int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {
        vector<vector<int>> grid(n, vector<int>(n, 1));
        for (vector<int>& m : mines) grid[m[0]][m[1]] = 0;

        vector<vector<PreComp>> preCalc(n, vector<PreComp>(n));
        vector<vector<int>> ansArr(n, vector<int>(n, 1));

        fillPfSf(preCalc, grid);

        int ans = 0;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i > 0 && i < n - 1 && j > 0 && j < n - 1 && grid[i][j] != 0) {
                    ansArr[i][j] = 1 + min({preCalc[i - 1][j].u, preCalc[i + 1][j].d, preCalc[i][j - 1].l, preCalc[i][j + 1].r});
                } else {
                    ansArr[i][j] = grid[i][j];
                }
                ans = max(ansArr[i][j], ans);
            }
        }

        return ans;
    }
};
95865