Problem · Matrix
Largest Sub-Grid
Given a square integer matrix grid and an integer maxSum, find the maximum side length of a square sub-grid whose total sum is at most maxSum.
If no square sub-grid satisfies the condition, return 0.
Examples
01 · Example 1
grid = [[2, 2, 2], [3, 3, 3], [4, 4, 4]] maxSum = 4 return = 0
Every 1 x 1 square in the last row sums to 4, but no larger square stays within the limit. Under the prompt's example, the maximum valid size is 0.
Constraints
gridis ann x nmatrix1 <= n <= 3001 <= maxSum <= 10^9
More MathWorks problems
public int largestSubGrid(int[][] grid, int maxSum) {
// write your code here
}
grid[[2, 2, 2], [3, 3, 3], [4, 4, 4]]
maxSum4
expected0
sign in to submit