Zepruv › Coding problems

Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Medium · Array, Binary Search, Matrix, Prefix Sum

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

The maximum side length of square with sum less than 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

Starter code (python)

class Solution(object):
    def maxSideLength(self, mat, threshold):
        """
        :type mat: List[List[int]]
        :type threshold: int
        :rtype: int
        """
        

Related problems

Previous: Find the Smallest Divisor Given a Threshold · Next: Find N Unique Integers Sum up to Zero · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.