Problem · Matrix

Largest Sub-Grid

MediumMathWorksFULLTIMEOA

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
  • grid is an n x n matrix
  • 1 <= n <= 300
  • 1 <= maxSum <= 10^9
More MathWorks problems
drafts saved locally
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