<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 m = matrix.size(); //row
        int n = matrix[0].size(); //columns
        // cout<<"m & n: "<<m<<" "<<n<<"\n";
        for(int i = 0; i<m; i++) {
            for(int j = 0; j<n; j++) {
                if(j) matrix[i][j] += matrix[i][j-1];
            }
        }

        int ans = INT_MIN;
        // choose two columns, and find contiguous row between these columns for maximum sum
        for(int i = 0; i< n; i++) {
            for(int j = i; j<n; j++) {
                // kadane's algo
                int curr = 0;
                vector<int> temp(m);
                for(int r = 0; r<m; r++) {
                    temp[r] = (i==0)? matrix[r][j]: (matrix[r][j] - matrix[r][i-1]);
                    // if(i == 2 && j == 2 ) {
                    //     cout<<temp[r]<<" ";
                    // }
                }
                // if(i == 2 && j == 2 ) {
                //     cout<<"\ndone\n";
                // }

                for(int r = 1; r<m; r++) {
                    temp[r] += temp[r-1];
                    // if(i == 2 && j == 2 ) {
                    //     cout<<temp[r]<<" ";
                    // }
                }
                // if(i == 2 && j == 2 ) {
                //     cout<<"\ndone done\n";
                // }

                // cout<<i<<" "<<j<<" temp:\n";
                // for(int r = 0; r<m; r++) {
                //     cout<<temp[r]<<" ";
                // }
                // cout<<"\n";

                set<int> st;
                for(auto x: temp) {
                    auto it = st.lower_bound(x-k);
                    if(it != st.end()) {
                        ans = max(ans, x - *it);
                    }
                    st.insert(x);
                    if(x<=k) ans = max(ans, x);
                }

                // ans = max(ans, mergeSort(i, j, 0, m-1, temp, k));

                // merge sort
            }
        }
        return ans;
    }

    int mergeSort(int p, int q, int start, int end, vector<int> &temp, int val) {
        int mid = start + (end - start)/2;
        // int n = temp.size();
        if(end - start <= 0) {
            if(end < start) return INT_MIN;
            if(temp[start] <= val) return temp[start];
            return INT_MIN;
        }
        int ans = INT_MIN;
        ans = max(ans, mergeSort(p, q, start, mid, temp, val));
        ans = max(ans, mergeSort(p, q, mid+1, end, temp, val));
        ans = max(ans, merge(p, q, start, mid, end, temp, val));

        return ans;
    }

    int merge(int p, int q, int start, int mid, int end, vector<int> &num, int val) {
        if(p == 2 && q == 2 && start == 4 && end == 7) {
            for(auto pp: num) cout<<pp<<" ";
            cout<<"----\n";
        }
        vector<int> temp(end - start + 1);
        int i = start, j = mid+1, k = 0;

        // cout<<"----------\n";
        // for(auto xx: num) cout<<xx<<" ";
        // cout<<"\n";
        
        int a = 0;
        int ans = INT_MIN;
        while(i <= mid) {
            bool found = false;
            // while(j+a <= end && num[j+a] - ( (i== 0)? 0 : num[i]) <= val) a++;
            while(j+a <= end && num[j+a] - num[i] <= val) {a++; found = true;}

            if(found) a--; // last element lesser or equal to val
            // if(found && j+a <= end && num[j+a] - num[i] <= val) ans = max(ans, num[j+a] - num[i]);
            if(found) {
                ans = max(ans, num[j+a] - num[i]);
                cout<<"foundddddddd\n";
            }
            while(j<= end && num[j]<num[i]) {
                if(num[j] <= val) ans = max(ans, num[j]);
                temp[k++] = num[j++];
            }
            if(num[i] <= val) ans = max(ans, num[i]);
            temp[k++] = num[i++];
        }
        while(i<=mid) {
            if(num[i] <= val) ans = max(ans, num[i]);
            temp[k++] = num[i++];
        }
        while(j<=end) {
            if(num[j] <= val) ans = max(ans, num[j]);
            temp[k++] = num[j++];
        }
        for(int x = start; x<=end; x++) {
            num[x] = temp[x - start];
        }
        if(p == 2 && q == 2 && start == 4 && end == 7) {
            for(auto pp: num) cout<<pp<<" ";
            cout<<"\n"<<ans<<" heheh\n";
        }
        // cout<<p<<" "<<q<<" "<<start<<" "<<mid<<" "<<end<<" "<<ans<<"\n";
        return ans;
    }
};
223236