Given an m x n
binary matrix
filled with 0
's and 1
's, find the largest square containing only 1
's and return its area.
Example 1:
Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] Output: 4
Example 2:
Input: matrix = [["0","1"],["1","0"]] Output: 1
Example 3:
Input: matrix = [["0"]] Output: 0
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 300
matrix[i][j]
is '0'
or '1'
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the biggest square of ones in a grid involves checking every possible square, one by one. We systematically look at all potential square sizes and starting positions within the grid to see if they are valid squares composed entirely of ones.
Here's how the algorithm would work step-by-step:
def maximal_square_brute_force(matrix):
if not matrix:
return 0
rows = len(matrix)
cols = len(matrix[0])
max_side = 0
for row_index in range(rows):
for col_index in range(cols):
# Consider each cell as top-left
for side_length in range(1, min(rows - row_index, cols - col_index) + 1):
is_square = True
# Check if all cells are '1'
for i in range(row_index, row_index + side_length):
for j in range(col_index, col_index + side_length):
if matrix[i][j] == '0':
is_square = False
break
if not is_square:
break
# Update max_side if valid
if is_square:
max_side = max(max_side, side_length)
return max_side * max_side
The goal is to find the biggest square made of only 1s within a grid of 0s and 1s. Instead of checking every possible square size at every location, we use a technique to build up square sizes incrementally.
Here's how the algorithm would work step-by-step:
def maximal_square(matrix):
number_of_rows = len(matrix)
number_of_columns = len(matrix[0]) if number_of_rows > 0 else 0
size_of_largest_square = 0
# This table stores max square size ending at each cell.
square_sizes = [[0] * number_of_columns for _ in range(number_of_rows)]
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
if matrix[row_index][column_index] == '1':
# Base case: single cell is a square of size 1.
square_sizes[row_index][column_index] = 1
if row_index > 0 and column_index > 0:
# Find minimum size of neighbors to expand square.
square_sizes[row_index][column_index] = min(
square_sizes[row_index-1][column_index],
square_sizes[row_index][column_index-1],
square_sizes[row_index-1][column_index-1]
) + 1
# Track the largest square found.
size_of_largest_square = max(
size_of_largest_square,
square_sizes[row_index][column_index]
)
# Return area, which is the side length squared.
return size_of_largest_square * size_of_largest_square
Case | How to Handle |
---|---|
Null or empty matrix | Return 0 immediately since no square can exist. |
Matrix with zero rows or columns | Return 0 immediately as an empty matrix has no area. |
Matrix with a single row and single column containing a 1 | Return 1 because a 1x1 square exists. |
Matrix with a single row or column containing only 0s | Return 0 because no square of 1s exists. |
Matrix filled entirely with 0s | Return 0, as the largest square will have side length 0. |
Matrix filled entirely with 1s | Return the area of the entire matrix, which is a square with side min(rows, cols). |
Large matrix where the largest square is located in the bottom right corner. | The dynamic programming approach will correctly propagate the square size information from top-left to bottom-right. |
Integer overflow when calculating the area for very large matrices | Ensure intermediate area calculations use a larger data type (e.g., long) or consider returning the side length squared. |