|
| 1 | +# [Problem 1292: Maximum Side Length of a Square with Sum Less than or Equal to Threshold](https://leetcode.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +We need the largest side length L such that there exists an L x L sub-square whose sum <= threshold. The brute force: for every top-left and every possible size compute sum by summing elements — that's too slow (O(m * n * L^2)). A common trick for submatrix sums is a 2D prefix-sum (summed-area table) which lets you compute any sub-square sum in O(1). With prefix sums we can check all positions for a given L in O(m * n). Then we can either scan L from 1..min(m,n) (O(m*n*min)), or binary search L to reduce to O(m*n*log(min)). Binary search seems natural and efficient. Need to be careful with indices and off-by-one in prefix sums. Also consider small edge cases: threshold may be 0, matrix values up to 1e4, but Python ints handle sums fine. |
| 5 | + |
| 6 | +## Refining the problem, round 2 thoughts |
| 7 | +Refinement: |
| 8 | +- Build prefix sum array ps of size (m+1)x(n+1) where ps[i][j] is sum of mat[:i][:j] (i and j are counts). |
| 9 | +- For given side k, iterate bottom-right corner (i, j) in ps coordinates with i from k..m and j from k..n. Sum of square with bottom-right at (i,j) and size k is: |
| 10 | + sum = ps[i][j] - ps[i-k][j] - ps[i][j-k] + ps[i-k][j-k] |
| 11 | + Check <= threshold. |
| 12 | +- Binary search over k in [1, min(m,n)]. If no k works, return 0. |
| 13 | +Edge cases: |
| 14 | +- If threshold is very small and no single cell <= threshold, return 0. |
| 15 | +- If mat dimensions up to 300, prefix sum O(m*n) memory and time are fine. |
| 16 | +Time / space: |
| 17 | +- Building prefix sums: O(m*n) time, O(m*n) space. |
| 18 | +- Each check for k: O(m*n). |
| 19 | +- Binary search over up to log(min(m,n)) iterations: overall O(m*n*log(min(m,n))). |
| 20 | +This is acceptable: at worst 300*300*~9 ≈ 810k checks. |
| 21 | + |
| 22 | +## Attempted solution(s) |
| 23 | +```python |
| 24 | +from typing import List |
| 25 | + |
| 26 | +class Solution: |
| 27 | + def maxSideLength(self, mat: List[List[int]], threshold: int) -> int: |
| 28 | + m = len(mat) |
| 29 | + n = len(mat[0]) if m > 0 else 0 |
| 30 | + if m == 0 or n == 0: |
| 31 | + return 0 |
| 32 | + |
| 33 | + # Build (m+1) x (n+1) prefix sum array |
| 34 | + ps = [[0] * (n + 1) for _ in range(m + 1)] |
| 35 | + for i in range(1, m + 1): |
| 36 | + row_sum = 0 |
| 37 | + for j in range(1, n + 1): |
| 38 | + row_sum += mat[i-1][j-1] |
| 39 | + ps[i][j] = ps[i-1][j] + row_sum |
| 40 | + |
| 41 | + # Helper to check if there exists a k x k square with sum <= threshold |
| 42 | + def exists_square_of_size(k: int) -> bool: |
| 43 | + if k == 0: |
| 44 | + return True |
| 45 | + for i in range(k, m + 1): |
| 46 | + for j in range(k, n + 1): |
| 47 | + total = ps[i][j] - ps[i-k][j] - ps[i][j-k] + ps[i-k][j-k] |
| 48 | + if total <= threshold: |
| 49 | + return True |
| 50 | + return False |
| 51 | + |
| 52 | + lo, hi = 1, min(m, n) |
| 53 | + best = 0 |
| 54 | + # Binary search over side length |
| 55 | + while lo <= hi: |
| 56 | + mid = (lo + hi) // 2 |
| 57 | + if exists_square_of_size(mid): |
| 58 | + best = mid |
| 59 | + lo = mid + 1 |
| 60 | + else: |
| 61 | + hi = mid - 1 |
| 62 | + |
| 63 | + return best |
| 64 | +``` |
| 65 | +- Notes: |
| 66 | + - Approach: build 2D prefix sums to compute any square sum in O(1). Binary search the side length to reduce checks. |
| 67 | + - Time complexity: O(m * n * log(min(m, n))) — building prefix sums O(m*n), each binary-search step checks all possible positions O(m*n). |
| 68 | + - Space complexity: O(m * n) for the prefix-sum array ps. |
| 69 | + - Implementation detail: ps is sized (m+1)x(n+1) to simplify boundary handling; indices in mat are offset by 1 when using ps. |
0 commit comments