Zepruv › Coding problems

Max Sum of Rectangle No Larger Than K

Hard · Array, Binary Search, Matrix, Prefix Sum, Ordered Set

Given an m x n matrix matrix and an integer k, return the max sum of a rectangle in the matrix such that its sum is no larger than k.
It is guaranteed that there will be a rectangle with a sum no larger than k.

Example 1

Input: matrix = [[1,0,1],[0,-2,3]], k = 2
Output: 2

Because the sum of the blue rectangle [[0, 1], [-2, 3]] is 2, and 2 is the max number no larger than k (k = 2).

Example 2

Input: matrix = [[2,2,-1]], k = 3
Output: 3

Constraints

Starter code (python)

class Solution(object):
    def maxSumSubmatrix(self, matrix, k):
        """
        :type matrix: List[List[int]]
        :type k: int
        :rtype: int
        """
        

Related problems

Previous: Count Numbers with Unique Digits · Next: Valid Perfect Square · 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.