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

class Solution {
public:
    int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {
        int maxSum = INT_MIN;
        int m = matrix.size();
        int n = matrix[0].size();
        set<int> sortedSum;
        if (m < n) {
            for (int i = 0; i < m; i++) {
                vector<int> arr(n, 0);
                for (int j = i; j < m; j++) {
                    for (int k = 0; k < n; k++)
                        arr[k] += matrix[j][k];
                    sortedSum.insert(0);
                    int maxSumSubArray = getMaxSumSubArray(arr, sortedSum, k);
                    if (maxSum < maxSumSubArray)
                        maxSum = maxSumSubArray;
                    sortedSum.clear();
                }
            }
        } else {
            for (int i = 0; i < n; i++) {
                vector<int> arr(m, 0);
                for (int j = i; j < n; j++) {
                    for (int k = 0; k < m; k++)
                        arr[k] += matrix[k][j];
                    sortedSum.insert(0);
                    int maxSumSubArray = getMaxSumSubArray(arr, sortedSum, k);
                    if (maxSum < maxSumSubArray)
                        maxSum = maxSumSubArray;
                    sortedSum.clear();
                }
            }
        }
        return maxSum;
    }

    int getMaxSumSubArray(vector<int>& arr, set<int>& sortedSum, int k) {
        int runningSum = 0;
        int maxSumSubArray, maxSumTillI;
        maxSumSubArray = INT_MIN;
        for (int i = 0; i < arr.size(); i++) {
            runningSum += arr[i];
            int x = runningSum - k;
            auto ans = sortedSum.lower_bound(x);
            int maxSumTillI = runningSum - *ans;
            sortedSum.insert(runningSum);
            if (ans == sortedSum.end())
                continue;
            if (maxSumTillI > maxSumSubArray && maxSumTillI <= k)
                maxSumSubArray = maxSumTillI;
        }
        return maxSumSubArray;
    }
};

/*
   i = 0
   arr = {0, 0, 0}
   j = 0, j < 2
   [1  0  1]
   set: {0}
   RS = 1
   X = 0 - 2
   ans = 0
   maxSumTillI = RS - ans = 1
   maxSum = 1
   {0, 1}


*/
124556