<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>

from sortedcontainers import SortedList, SortedSet, SortedDict 

class Solution:
    def maxSumSubmatrix(self, matrix: List[List[int]], k: int) -> int:
        m, n = len(matrix), len(matrix[0])
        prefixSums = [[0] * n for _ in range(m)]
        for i in range(m):
            for j in range(n):
                prefixSums[i][j] = (prefixSums[i][j - 1] if j > 0 else 0) + matrix[i][j]
        max_sum = -inf
        for c1 in range(n):
            for c2 in range(c1, n):
                prefs = SortedList()
                prefs.add(0)
                prefixSum = 0
                for i in range(m):
                    prefixSum += prefixSums[i][c2] - (prefixSums[i][c1 - 1] if c1 > 0 else 0)
                    # find first prefixSum >= target
                    l, r = 0, len(prefs) - 1
                    target = prefixSum - k
                    while l <= r:
                        mid = l + (r - l) // 2
                        if prefs[mid] < target:
                            l = mid + 1
                        elif prefs[mid] > target:
                            r = mid - 1
                        else:
                            return k
                    if l < len(prefs):
                        max_sum = max(max_sum, prefixSum - prefs[l])
                    prefs.add(prefixSum)
        return max_sum

    # def maxSumSubmatrix(self, matrix: List[List[int]], k: int) -> int:
    #     m, n = len(matrix), len(matrix[0])
    #     prefixSums = [[0] * n for _ in range(m)]
    #     for j in range(n):
    #         for i in range(m):
    #             prefixSums[i][j] = (prefixSums[i - 1][j] if i > 0 else 0) + matrix[i][j]
    #     max_sum = -inf
    #     for r1 in range(m):
    #         for r2 in range(r1, m):
    #             prefs = SortedList()
    #             prefs.add(0)
    #             prefixSum = 0
    #             for j in range(n):
    #                 prefixSum += prefixSums[r2][j] - (prefixSums[r1 - 1][j] if r1 > 0 else 0)
    #                 # find first prefixSum >= target
    #                 l, r = 0, len(prefs) - 1
    #                 target = prefixSum - k
    #                 while l <= r:
    #                     mid = l + (r - l) // 2
    #                     if prefs[mid] < target:
    #                         l = mid + 1
    #                     elif prefs[mid] > target:
    #                         r = mid - 1
    #                     else:
    #                         return k
    #                 if l < len(prefs):
    #                     max_sum = max(max_sum, prefixSum - prefs[l])
    #                 prefs.add(prefixSum)
    #     return max_sum

17900