<p>Given an <code>m x n</code> matrix <code>matrix</code> and an integer <code>k</code>, return <em>the max sum of a rectangle in the matrix such that its sum is no larger than</em> <code>k</code>.</p>

<p>It is <strong>guaranteed</strong> that there will be a rectangle with a sum no larger than <code>k</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/18/sum-grid.jpg" style="width: 255px; height: 176px;" />
<pre>
<strong>Input:</strong> matrix = [[1,0,1],[0,-2,3]], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> 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).
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> matrix = [[2,2,-1]], k = 3
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == matrix.length</code></li>
	<li><code>n == matrix[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 100</code></li>
	<li><code>-100 &lt;= matrix[i][j] &lt;= 100</code></li>
	<li><code>-10<sup>5</sup> &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> What if the number of rows is much larger than the number of columns?</p>

import bisect
class Solution:
    def maxSumSubmatrix(self, matrix: List[List[int]], k: int) -> int:
        prefixMatrix = []
        m, n = len(matrix), len(matrix[0])
        self.m = m
        self.n = n
        pre = []
        for i in range(0, m):
            tmp = []
            cumu = 0
            for j in range(0, n):
                if i == 0:
                    cumu += matrix[i][j]
                    tmp.append(cumu)
                else:
                    cumu += matrix[i][j]
                    tmp.append(pre[j] + cumu)
            pre = tmp
            prefixMatrix.append(tmp)        
        self.prefixMatrix = prefixMatrix

        res = -2**31
        # we fix two y axis (x position), then do prefix sum
        for x1 in range(0, m):
            for x2 in range(x1, m):
                col_sums = []
                for col in range(0, n):
                    rec_sum = self.calc(x1,0,x2,col)
                    comp = rec_sum - k

                    if rec_sum <= k:
                        res = max(res, rec_sum)
                    
                    compidx = bisect.bisect_left(col_sums, comp)
                    if col_sums and compidx < len(col_sums):
                        if rec_sum - col_sums[compidx] <= k:
                            res = max(res, rec_sum - col_sums[compidx])

                    bisect.insort(col_sums, rec_sum)
        return res
                    

    def maxSumSubmatrixNaive(self, matrix: List[List[int]], k: int) -> int:
        prefixMatrix = []
        m, n = len(matrix), len(matrix[0])
        self.m = m
        self.n = n
        pre = []
        for i in range(0, m):
            tmp = []
            cumu = 0
            for j in range(0, n):
                if i == 0:
                    cumu += matrix[i][j]
                    tmp.append(cumu)
                else:
                    cumu += matrix[i][j]
                    tmp.append(pre[j] + cumu)
            pre = tmp
            prefixMatrix.append(tmp)        
        self.prefixMatrix = prefixMatrix

        res = -9999999
        for a in range(0, m):
            for b in range(0, n):
                for c in range(a, m):
                    for d in range(b, n):
                        tmp = self.calc(a,b,c,d)
                        if tmp <= k:
                            if tmp == k:
                                return k 
                            res = max(res, tmp)

        return res

    def get(self, a, b):
        if a < 0 or b < 0 or a >= self.m or b >= self.n:
            return 0
        return self.prefixMatrix[a][b]
    
    def calc(self, a, b, c, d):
        return self.get(c,d) + self.get(a-1, b-1) - self.get(c, b-1) - self.get(a-1, d)

        
17600