Given a m x n matrix mat and an integer threshold, return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.
Example 1:
Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4 Output: 2 Explanation: The maximum side length of square with sum less than 4 is 2 as shown.
Example 2:
Input: mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1 Output: 0
Constraints:
m == mat.lengthn == mat[i].length1 <= m, n <= 3000 <= mat[i][j] <= 1040 <= threshold <= 105When 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 involves checking every possible square that could fit within the given grid. For each potential square, we calculate the sum of all the numbers inside it and see if that sum is within the allowed limit.
Here's how the algorithm would work step-by-step:
def max_side_length_brute_force(mat, threshold):
rows_in_grid = len(mat)
columns_in_grid = len(mat[0])
maximum_side_found = 0
# Iterate through every possible top-left corner of a square
for top_row_index in range(rows_in_grid):
for left_column_index in range(columns_in_grid):
# Try all possible square sizes from this corner
for side_length in range(1, min(rows_in_grid - top_row_index, columns_in_grid - left_column_index) + 1):
bottom_row_index = top_row_index + side_length - 1
right_column_index = left_column_index + side_length - 1
current_square_sum = 0
# Calculate the sum of elements within the current square
for row_to_sum in range(top_row_index, bottom_row_index + 1):
for column_to_sum in range(left_column_index, right_column_index + 1):
current_square_sum += mat[row_to_sum][column_to_sum]
# Check if the sum is within the threshold
if current_square_sum <= threshold:
# Update the maximum side length if this square is valid and larger
maximum_side_found = max(maximum_side_found, side_length)
return maximum_side_foundThe problem asks for the largest square within a grid where the sum of all the numbers inside that square is no more than a given limit. The smart approach involves pre-calculating sums of rectangular areas to quickly find the sum of any square, and then efficiently searching for the largest possible square.
Here's how the algorithm would work step-by-step:
def maximum_side_length(matrix, threshold):
rows_in_matrix = len(matrix)
columns_in_matrix = len(matrix[0])
# Pre-calculate prefix sums to quickly query rectangular sums.
prefix_sum_grid = [[0] * (columns_in_matrix + 1) for _ in range(rows_in_matrix + 1)]
for row_index in range(rows_in_matrix):
for column_index in range(columns_in_matrix):
prefix_sum_grid[row_index + 1][column_index + 1] = (
matrix[row_index][column_index]
+ prefix_sum_grid[row_index][column_index + 1]
+ prefix_sum_grid[row_index + 1][column_index]
- prefix_sum_grid[row_index][column_index]
)
def get_square_sum(row, col, side_length):
# Efficiently calculate sum of a square using prefix sums.
bottom_row = row + side_length
right_column = col + side_length
return (
prefix_sum_grid[bottom_row][right_column]
- prefix_sum_grid[row][right_column]
- prefix_sum_grid[bottom_row][col]
+ prefix_sum_grid[row][col]
)
def can_form_square(side_length):
# Check if any square of the given side_length meets the threshold.
for row_index in range(rows_in_matrix - side_length + 1):
for column_index in range(columns_in_matrix - side_length + 1):
if get_square_sum(row_index, column_index, side_length) <= threshold:
return True
return False
# Binary search for the largest possible side length.
low_side = 0
high_side = min(rows_in_matrix, columns_in_matrix)
maximum_valid_side = 0
while low_side <= high_side:
mid_side = (low_side + high_side) // 2
if mid_side == 0:
# A square of side 0 is always possible and has sum 0.
low_side = mid_side + 1
continue
if can_form_square(mid_side):
# If a square of mid_side works, try for a larger one.
maximum_valid_side = mid_side
low_side = mid_side + 1
else:
# If a square of mid_side does not work, we need a smaller one.
high_side = mid_side - 1
return maximum_valid_side| Case | How to Handle |
|---|---|
| Empty or null matrix input | The solution should handle null or empty matrix inputs by returning 0 as no square can be formed. |
| Matrix with a single element | If the single element is less than or equal to the threshold, the maximum side length is 1, otherwise it's 0. |
| Matrix dimensions are 1xN or Nx1 | The logic should correctly identify that only 1x1 squares are possible in such cases. |
| Threshold is very small (e.g., negative or zero) | If the threshold is non-positive, and all matrix elements are positive, no valid square will exist, and the result should be 0. |
| All elements in the matrix are zero | Any square size is valid if the threshold is non-negative; the maximum possible side length should be returned. |
| All elements in the matrix are very large | If even a 1x1 square's sum exceeds the threshold, the answer must be 0. |
| No square satisfies the threshold condition | The algorithm should correctly determine that no valid square exists and return 0. |
| The entire matrix forms a valid square | The algorithm should be able to find the maximum side length equal to the minimum dimension of the matrix if the whole matrix sum is within the threshold. |