Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Given an 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
1 1 3 2 4 3 2
1 1 3 2 4 3 2
1 1 3 2 4 3 2
Inputmat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
Output2
The maximum side length of a square with sum less than or equal to 4 is 2.
Example 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 2
Inputmat = [[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
Output0
Every non-empty square has sum greater than 1, so no valid square exists.

Constraints

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 300
  • 0 <= mat[i][j] <= 10^4
  • 0 <= threshold <= 10^5

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate