1413 Maximum Side Length Of A Square With Sum Less Than Or Equal To Threshold
1413 Maximum Side Length Of A Square With Sum Less Than Or Equal To Threshold
Maximum Side Length of a Square with Sum Less than or Equal to Threshold 
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:
1
2
3
4
5
**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:
1
2
3
4
**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:
1
2
3
4
5
m == mat.length
n == mat[i].length
1 <= m, n <= 300
0 <= mat[i][j] <= 104
0 <= threshold <= 105
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution:
def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
m, n = len(mat), len(mat[0])
P = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
P[i][j] = (
P[i - 1][j]
+ P[i][j - 1]
- P[i - 1][j - 1]
+ mat[i - 1][j - 1]
)
def getRect(x1, y1, x2, y2):
return P[x2][y2] - P[x1 - 1][y2] - P[x2][y1 - 1] + P[x1 - 1][y1 - 1]
l, r, ans = 1, min(m, n), 0
while l <= r:
mid = (l + r) // 2
find = any(
getRect(i, j, i + mid - 1, j + mid - 1) <= threshold
for i in range(1, m - mid + 2)
for j in range(1, n - mid + 2)
)
if find:
ans = mid
l = mid + 1
else:
r = mid - 1
return ans
This post is licensed under CC BY 4.0 by the author.
