1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
Medium65.4% acceptance123,473 / 188,657 submissions
Asked by 5 companies
Topics
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 or equal to 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 <= 105
Hints
Hint 1
Store prefix sum of all grids in another 2D array.
Hint 2
Try all possible solutions and if you cannot find one return 0.
Hint 3
If x is a valid answer then any y < x is also valid answer. Use binary search to find answer.