Largest 1-Bordered Square - Complete Solution Guide
Largest 1-Bordered Square is LeetCode problem 1139, 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
Given a 2D grid of 0 s and 1 s, return the number of elements in the largest square subgrid that has all 1 s on its border , or 0 if such a subgrid doesn't exist in the grid . Example 1: Input: grid = [[1,1,1],[1,0,1],[1,1,1]] Output: 9 Example 2: Input: grid = [[1,1,0,0]] Output: 1 Constraints: 1 <= grid.length <= 100 1 <= grid[0].length <= 100 grid[i][j] is 0 or 1
Detailed Explanation
The problem asks us to find the largest square subgrid within a given 2D grid of 0s and 1s, such that all elements on the border of the square are 1s. The function should return the number of elements in this largest square (which is the side length squared). If no such square exists (i.e., the grid contains no 1s that form a bordered square), the function should return 0.
Solution Approach
The provided solution uses dynamic programming to precompute the lengths of consecutive 1s to the right (horizontally) and downwards (vertically) for each cell in the grid. Then, it iterates through the grid from the bottom-right corner, checking for possible squares with decreasing side lengths. For each cell, it determines the maximum possible side length of a square that could end at that cell based on the horizontal and vertical lengths. It then checks if a square of that side length exists by looking at the top and left borders. If such a square is found, it updates the maximum side length and continues until a square is found, then breaks to search the remaining grid.
Step-by-Step Algorithm
- Step 1: Initialize two 2D arrays, `hor` and `ver`, to store the horizontal and vertical lengths of consecutive 1s, respectively. These arrays have the same dimensions as the input `grid`.
- Step 2: Iterate through the `grid` from left to right, top to bottom. For each cell `grid[r][c]`, if it's a 1, update `hor[r][c]` with the length of consecutive 1s to its left (including itself) and update `ver[r][c]` with the length of consecutive 1s above it (including itself).
- Step 3: Initialize `max_side` to 0, representing the side length of the largest square found so far.
- Step 4: Iterate through the `grid` from bottom to top, right to left. For each cell `grid[r][c]`, determine the potential side length `side` based on the minimum of `hor[r][c]` and `ver[r][c]`. The potential side length represents the largest square that could end at that cell.
- Step 5: Check if a square of side length `side` exists. This is done by checking if the top border (starting at `grid[r - side + 1][c]`) and the left border (starting at `grid[r][c - side + 1]`) also have at least `side` consecutive 1s. We do this by seeing if hor[r - side + 1][c] >= side and ver[r][c - side + 1] >= side.
- Step 6: If a valid square is found, update `max_side` to `side` and break out of the inner `while` loop since you've found the largest square ending at this cell.
- Step 7: After iterating through the entire `grid`, return `max_side * max_side`, which is the area of the largest 1-bordered square.
Key Insights
- Insight 1: We need to efficiently determine the length of consecutive 1s horizontally and vertically at each cell to check if a square can be formed.
- Insight 2: Dynamic programming can be used to precompute the horizontal and vertical lengths of consecutive 1s, enabling faster checks for square existence.
- Insight 3: We can iterate through potential square sizes starting from the largest possible and decrementing, which allows us to stop as soon as we find a valid square.
Complexity Analysis
Time Complexity: O(m*n*min(m,n))
Space Complexity: O(m*n)
Topics
This problem involves: Array, Dynamic Programming, Matrix.
Companies
Asked at: DE Shaw, Samsung, ZS Associates.