LeetCode //C - 764. Largest Plus Sign

764. Largest Plus Sign

You are given an integer n. You have an n x n binary grid grid with all values initially 1’s except for some indices given in the array mines. The i t h i^{th} ith element of the array mines is defined as m i n e s [ i ] = [ x i , y i ] mines[i] = [x_i, y_i] mines[i]=[xi,yi] where g r i d [ x i ] [ y i ] = = 0 grid[x_i][y_i] == 0 grid[xi][yi]==0.

Return the order of the largest axis-aligned plus sign of 1’s contained in grid. If there is none, return 0.

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

Example 1:

在这里插入图片描述

Input: n = 5, mines = [[4,2]]
Output: 2
Explanation: In the above grid, the largest plus sign can only be of order 2. One of them is shown.

Example 2:

在这里插入图片描述

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

Constraints:
  • 1 <= n <= 500
  • 1 <= mines.length <= 5000
  • 0 < = x i , y i < n 0 <= x_i, y_i < n 0<=xi,yi<n
  • All the pairs ( x i , y i ) (x_i, y_i) (xi,yi) are unique.

From: LeetCode
Link: 764. Largest Plus Sign


Solution:

Ideas:

1. Grid Initialization: Create an n×n grid filled with 1s, then mark all mine positions as 0s.

2. Direction Arrays: Create four 2D arrays (left, right, up, down) to store the number of consecutive 1s extending in each direction from every position.

3. Calculate Consecutive 1s:

  • left[i][j]: Number of consecutive 1s from position (i,j) going left
  • right[i][j]: Number of consecutive 1s from position (i,j) going right
  • up[i][j]: Number of consecutive 1s from position (i,j) going up
  • down[i][j]: Number of consecutive 1s from position (i,j) going down

4. Find Maximum Plus Sign: For each position that contains a 1, calculate the potential plus sign order by taking the minimum of all four directions. The plus sign order is this minimum value.

5. Memory Management: Properly free all allocated memory.

Code:
int orderOfLargestPlusSign(int n, int** mines, int minesSize, int* minesColSize) {
    // Create grid with all 1s initially
    int** grid = (int**)malloc(n * sizeof(int*));
    for (int i = 0; i < n; i++) {
        grid[i] = (int*)malloc(n * sizeof(int));
        for (int j = 0; j < n; j++) {
            grid[i][j] = 1;
        }
    }
    
    // Mark mines as 0
    for (int i = 0; i < minesSize; i++) {
        grid[mines[i][0]][mines[i][1]] = 0;
    }
    
    // Create arrays to store consecutive 1s in each direction
    int** left = (int**)malloc(n * sizeof(int*));
    int** right = (int**)malloc(n * sizeof(int*));
    int** up = (int**)malloc(n * sizeof(int*));
    int** down = (int**)malloc(n * sizeof(int*));
    
    for (int i = 0; i < n; i++) {
        left[i] = (int*)malloc(n * sizeof(int));
        right[i] = (int*)malloc(n * sizeof(int));
        up[i] = (int*)malloc(n * sizeof(int));
        down[i] = (int*)malloc(n * sizeof(int));
    }
    
    // Calculate consecutive 1s going left
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (grid[i][j] == 0) {
                left[i][j] = 0;
            } else {
                left[i][j] = (j == 0) ? 1 : left[i][j-1] + 1;
            }
        }
    }
    
    // Calculate consecutive 1s going right
    for (int i = 0; i < n; i++) {
        for (int j = n-1; j >= 0; j--) {
            if (grid[i][j] == 0) {
                right[i][j] = 0;
            } else {
                right[i][j] = (j == n-1) ? 1 : right[i][j+1] + 1;
            }
        }
    }
    
    // Calculate consecutive 1s going up
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < n; i++) {
            if (grid[i][j] == 0) {
                up[i][j] = 0;
            } else {
                up[i][j] = (i == 0) ? 1 : up[i-1][j] + 1;
            }
        }
    }
    
    // Calculate consecutive 1s going down
    for (int j = 0; j < n; j++) {
        for (int i = n-1; i >= 0; i--) {
            if (grid[i][j] == 0) {
                down[i][j] = 0;
            } else {
                down[i][j] = (i == n-1) ? 1 : down[i+1][j] + 1;
            }
        }
    }
    
    // Find the maximum plus sign order
    int maxOrder = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (grid[i][j] == 1) {
                int minArm = left[i][j];
                if (right[i][j] < minArm) minArm = right[i][j];
                if (up[i][j] < minArm) minArm = up[i][j];
                if (down[i][j] < minArm) minArm = down[i][j];
                
                if (minArm > maxOrder) {
                    maxOrder = minArm;
                }
            }
        }
    }
    
    // Free allocated memory
    for (int i = 0; i < n; i++) {
        free(grid[i]);
        free(left[i]);
        free(right[i]);
        free(up[i]);
        free(down[i]);
    }
    free(grid);
    free(left);
    free(right);
    free(up);
    free(down);
    
    return maxOrder;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值