Formatted question description: https://leetcode.ca/all/764.html

764. Largest Plus Sign

Level

Medium

Description

In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given list mines which are 0. What is the largest axis-aligned plus sign of 1s contained in the grid? Return the order of the plus sign. If there is none, return 0.

An “axis-aligned plus sign of 1s of order k” has some center grid[x][y] = 1 along with 4 arms of length k-1 going up, down, left, and right, and made of 1s. This is demonstrated in the diagrams below. Note that there could be 0s or 1s beyond the arms of the plus sign, only the relevant area of the plus sign is checked for 1s.

Examples of Axis-Aligned Plus Signs of Order k:

Order 1:
000
010
000

Order 2:
00000
00100
01110
00100
00000

Order 3:
0000000
0001000
0001000
0111110
0001000
0001000
0000000

Example 1:

Input: N = 5, mines = [[4, 2]]
Output: 2
Explanation:
11111
11111
11111
11111
11011
In the above grid, the largest plus sign can only be order 2.

Example 2:

Input: N = 2, mines = []
Output: 1
Explanation:
There is no plus sign of order 2, but there is of order 1.

Example 3:

Input: N = 1, mines = [[0, 0]]
Output: 0
Explanation:
There is no plus sign, so return 0.

Note:

  1. N will be an integer in the range [1, 500].
  2. mines will have length at most 5000.
  3. mines[i] will be length 2 and consist of integers in the range [0, N-1].
  4. (Additionally, programs submitted in C, C++, or C# will be judged with a slightly smaller time limit.)

Solution

Use dynamic programming. For each row, loop from left to right and loop from right to left to determine the maximum order of the plus sign with the current cell as the center. For each column, loop from top to bottom and loop from bottom to top to determine the maximum order of the plus sign with the current cell as the center. Finally, return the maximum order possible.

  • class Solution {
        public int orderOfLargestPlusSign(int N, int[][] mines) {
            int maxOrder = 0;
            Set<String> minesSet = new HashSet<String>();
            for (int[] mine : mines)
                minesSet.add(Arrays.toString(mine));
            int[][] dp = new int[N][N];
            for (int i = 0; i < N; i++) {
                int count = 0;
                for (int j = 0; j < N; j++) {
                    int[] cell = {i, j};
                    if (minesSet.contains(Arrays.toString(cell)))
                        count = 0;
                    else
                        count++;
                    dp[i][j] = count;
                }
                count = 0;
                for (int j = N - 1; j >= 0; j--) {
                    int[] cell = {i, j};
                    if (minesSet.contains(Arrays.toString(cell)))
                        count = 0;
                    else
                        count++;
                    dp[i][j] = Math.min(dp[i][j], count);
                }
            }
            for (int i = 0; i < N; i++) {
                int count = 0;
                for (int j = 0; j < N; j++) {
                    int[] cell = {j, i};
                    if (minesSet.contains(Arrays.toString(cell)))
                        count = 0;
                    else
                        count++;
                    dp[j][i] = Math.min(dp[j][i], count);
                }
                count = 0;
                for (int j = N - 1; j >= 0; j--) {
                    int[] cell = {j, i};
                    if (minesSet.contains(Arrays.toString(cell)))
                        count = 0;
                    else
                        count++;
                    dp[j][i] = Math.min(dp[j][i], count);
                    maxOrder = Math.max(maxOrder, dp[j][i]);
                }
            }
            return maxOrder;
        }
    }
    
  • // OJ: https://leetcode.com/problems/largest-plus-sign/
    // Time: O(N^2)
    // Space: O(N^2)
    class Solution {
    public:
        int orderOfLargestPlusSign(int N, vector<vector<int>>& A) {
            vector<vector<int>> M(N, vector<int>(N, 1)), H(N, vector<int>(N)), V(N, vector<int>(N));
            for (auto &v : A) {
                M[v[0]][v[1]] = 0;
            }
            for (int i = 0; i < N; ++i) {
                stack<int> s;
                s.push(N);
                for (int j = N - 1; j >= 0; --j) {
                    if (M[i][j] == 0) s.push(j);
                }
                int prev = -1;
                for (int j = 0; j < N; ++j) {
                    if (M[i][j] == 0) {
                        s.pop();
                        prev = j;
                    } else H[i][j] = min(j - prev, s.top() - j);
                }
            }
            for (int j = 0; j < N; ++j) {
                stack<int> s;
                s.push(N);
                for (int i = N - 1; i >= 0; --i) {
                    if (M[i][j] == 0) s.push(i);
                }
                int prev = -1;
                for (int i = 0; i < N; ++i) {
                    if (M[i][j] == 0) {
                        s.pop();
                        prev = i;
                    } else V[i][j] = min(i - prev, s.top() - i);
                }
            }
            int ans = 0;
            for (int i = 0; i < N; ++i) {
                for (int j = 0; j < N; ++j) {
                    ans = max(ans, min(H[i][j], V[i][j]));
                }
            }
            return ans;
        }
    };
    
  • class Solution:
        def orderOfLargestPlusSign(self, N, mines):
            """
            :type N: int
            :type mines: List[List[int]]
            :rtype: int
            """
            res = 0
            dp = [[0 for i in range(N)] for j in range(N)]
            s = set()
            for mine in mines:
                s.add(N * mine[0] + mine[1])
            for i in range(N):
                cnt = 0
                for j in range(N):#left
                    cnt = 0 if N * i + j in s else cnt + 1
                    dp[i][j] = cnt
                cnt = 0
                for j in range(N - 1, -1, -1):#right
                    cnt = 0 if N * i + j in s else cnt + 1
                    dp[i][j] = min(dp[i][j], cnt)
            for j in range(N):
                cnt = 0
                for i in range(N):#up
                    cnt = 0 if N * i + j in s else cnt + 1
                    dp[i][j] = min(dp[i][j], cnt)
                cnt = 0
                for i in range(N - 1, -1, -1):#down
                    cnt = 0 if N * i + j in s else cnt + 1
                    dp[i][j] = min(dp[i][j], cnt)
                    res = max(dp[i][j], res)
            return res
    

All Problems

All Solutions