HTMLify
maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold.py
Views: 2 | Author: prakhardoneria
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Solution: def maxSideLength(self, mat: List[List[int]], threshold: int) -> int: m, n = len(mat), len(mat[0]) pref = [[0] * (n + 1) for _ in range(m + 1)] for r in range(1, m + 1): for c in range(1, n + 1): pref[r][c] = mat[r-1][c-1] + pref[r-1][c] + pref[r][c-1] - pref[r-1][c-1] res = 0 for r in range(1, m + 1): for c in range(1, n + 1): while r + res <= m and c + res <= n: if pref[r+res][c+res] - pref[r-1][c+res] - pref[r+res][c-1] + pref[r-1][c-1] <= threshold: res += 1 else: break return res |