Largest Plus Sign - Complete Solution Guide
Largest Plus Sign is LeetCode problem 764, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 th element of the array mines is defined as mines[i] = [x i , y i ] where grid[x i ][y i ] == 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, an
Detailed Explanation
The problem asks us to find the largest 'plus sign' of 1s within an n x n grid. Initially, all cells in the grid are 1s. However, the input `mines` array specifies certain cell coordinates that should be set to 0. The 'order' of a plus sign is determined by the length of its arms (up, down, left, right) extending from a central cell, all of which must consist of 1s. A plus sign of order `k` has arms of length `k-1`. The goal is to find the maximum `k` such that a plus sign of order `k` exists entirely within the grid, composed only of 1s after applying the `mines`. If no such plus sign exists, return 0.
Solution Approach
The solution uses dynamic programming to efficiently determine the order of the largest plus sign. It creates a DP table of the same size as the grid. Each cell in the DP table will store the maximum possible order of a plus sign centered at that cell. The algorithm iterates through the grid four times: from left to right, right to left, top to bottom, and bottom to top. In each iteration, it calculates the length of consecutive 1s in the corresponding direction. Finally, the algorithm iterates through the grid again and takes the minimum of the four directional counts for each cell. This minimum represents the maximum possible order of a plus sign centered at that cell. The largest of these values is the final result.
Step-by-Step Algorithm
- Step 1: Initialize a set `banned` to store the coordinates of the mined cells. This allows for efficient checking whether a cell is a mine (0) or not (1).
- Step 2: Create a DP table `dp` of size n x n, initialized with 0s. This table will store the maximum order of a plus sign that can be centered at each cell.
- Step 3: Iterate through each row from left to right. For each cell, if it is not a mine, increment the `count`. Otherwise, reset the `count` to 0. Store the `count` in the DP table.
- Step 4: Iterate through each row from right to left. For each cell, if it is not a mine, increment the `count`. Otherwise, reset the `count` to 0. Update the DP table with the minimum of the current value and the `count`.
- Step 5: Iterate through each column from top to bottom. For each cell, if it is not a mine, increment the `count`. Otherwise, reset the `count` to 0. Update the DP table with the minimum of the current value and the `count`.
- Step 6: Iterate through each column from bottom to top. For each cell, if it is not a mine, increment the `count`. Otherwise, reset the `count` to 0. Update the DP table with the minimum of the current value and the `count`.
- Step 7: Iterate through the DP table and find the maximum value. This value represents the order of the largest plus sign.
Key Insights
- Insight 1: Dynamic programming is suitable here because we can calculate the length of consecutive 1s in each direction (up, down, left, right) from each cell and reuse this information to determine the maximum plus sign order.
- Insight 2: Preprocessing the grid by calculating the lengths of consecutive 1s simplifies the problem significantly compared to brute-force checking all possible plus signs.
- Insight 3: Storing the banned cells (mines) in a hash set allows for quick lookups when determining whether a cell contains a 1 or a 0.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n^2)
Topics
This problem involves: Array, Dynamic Programming.
Companies
Asked at: Intuit.