Java implements LeetCode 764 maximum plus sign (violent recursion)

764. Maximum plus sign

In a 2D grid grid with sizes ranging from (0, 0) to (N-1, N-1), each cell is 1 except that the cell given in mines is 0. What is the largest axis-aligned plus sign that contains 1 in the grid? Returns the order of the plus sign. If the plus sign is not found, it returns 0.

A k "order" axisymmetric "plus sign consisting of 1 has a center grid grid [x] [y] = 1 and four extensions extending up, down, left, and right from the center, with a length of k- 1. An arm consisting of 1. An example of the "axisymmetric" plus sign of the k "order is given below. Note that all grids marked with a plus sign are required to be 1, other grids may be 0 or 1.

Example of axisymmetric plus sign for order k:

Stage 1:
000
010
000

Stage 2:
00000
00100
01110
00100
00000

Step 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 upper grid, the order of the largest plus sign can only be 2. A sign has been marked in the figure.

Example 2:

Input: N = 2, mines = []
Output: 1
Explanation:

11
11

There is no 2nd order plus sign, there is 1st order plus sign.

Example 3:

Input: N = 1, mines = [[0, 0]]
Output: 0
Explanation:

0

Without the plus sign, return 0.

prompt:

The range of integer N: [1, 500].
The maximum length of
mines is 5000. mines [i] is a length of 2 and consists of 2 numbers in [0, N-1].
(In addition, use C, C ++, Or C # programming will be judged with a slightly smaller time limit.)

class Solution { 
     
    public int orderOfLargestPlusSign(int N, int[][] mines) {
        if (N==0)return 0;

        int max_k=0;

        //填充一个2D网格matrix出来
        int matrix[][] = new int[N][N];
        for (int i=0;i<N;i++)
            Arrays.fill(matrix[i],1);
        int m = mines.length;
        for (int i=0;i<m;i++)
            matrix[mines[i][0]][mines[i][1]]=0;

        int dp[][] = new int[N][N];
        int count = 0;
        //初始化
        //求左、右
        for (int i=0;i<N;i++){

            //求左臂并将值放入dp left->right
            count = 0;
            for (int j=0;j<N;j++){
                count = matrix[i][j]==1?count+1:0;
                dp[i][j]= count;
            }

            //求右臂 并将当前dp与右臂之间的最小值放入dp right->left
            count = 0;
            for (int j=N-1;j>=0;j--){
                count = matrix[i][j]==1?count+1:0;
                dp[i][j]=Math.min(dp[i][j],count);
            }
        }
        //求上、下
        for (int j=0;j<N;j++){
            //求上臂 并将当前dp与上臂间的最小值放入dp up->down
            count = 0;
            for (int i=0;i<N;i++){
                count = matrix[i][j]==1?count+1:0;
                dp[i][j]=Math.min(dp[i][j],count);
            }
            //求下臂 并将当前dp与下臂间的最小值放入dp down->up
            //此时的整个dp数组中的最大值就是所求的k
            count = 0;
            for (int i=N-1;i>=0;i--){
                count = matrix[i][j]==1?count+1:0;
                dp[i][j]=Math.min(dp[i][j],count);
                max_k=Math.max(max_k,dp[i][j]);
            }
        }

        return max_k;
    }
}
1833 original articles published · 30,000 likes + · 4.86 million views

Guess you like

Origin blog.csdn.net/a1439775520/article/details/105557981