MediumNo. 1292

Maximum Side Length of a Square with Sum Less than or Equal to Threshold — LeetCode 1292 Python Solution

Pattern
Prefix Sum
Topics
Array, Binary Search, Matrix, Prefix Sum
Reading time
4 min

The problem

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

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.

Python solution

PythonSolution
class Solution:
    def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
        def check(k: int) -> bool:
            for i in range(m - k + 1):
                for j in range(n - k + 1):
                    v = s[i + k][j + k] - s[i][j + k] - s[i + k][j] + s[i][j]
                    if v <= threshold:
                        return True
            return False

        m, n = len(mat), len(mat[0])
        s = [[0] * (n + 1) for _ in range(m + 1)]
        for i, row in enumerate(mat, 1):
            for j, x in enumerate(row, 1):
                s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + x
        l, r = 0, min(m, n)
        while l < r:
            mid = (l + r + 1) >> 1
            if check(mid):
                l = mid
            else:
                r = mid - 1
        return l

Complexity

Time
O(m × n × log min(m, n))
Space · auxiliary
O(m × n)

Pattern: Prefix Sum

Precompute running totals once so any range query becomes a single subtraction. LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold is filed here because LeetCode tags it Prefix Sum, which is the vocabulary this hub collects.

The prefix sum guide has the Python template for the pattern and the 157 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold?

LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold is rated Medium on LeetCode.

What is the time complexity of LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold?

The Python solution on this page runs in O(m × n × log min(m, n)).

What is the space complexity of LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold?

The Python solution on this page uses O(m × n) auxiliary space.

What topics does LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold cover?

LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold is tagged Array, Binary Search, Matrix and Prefix Sum on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.