<p>You are given <code>m</code> <code>arrays</code>, where each array is sorted in <strong>ascending order</strong>.</p>

<p>You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers <code>a</code> and <code>b</code> to be their absolute difference <code>|a - b|</code>.</p>

<p>Return <em>the maximum distance</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arrays = [[1,2,3],[4,5],[1,2,3]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
</pre>

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

<pre>
<strong>Input:</strong> arrays = [[1],[1]]
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>m == arrays.length</code></li>
	<li><code>2 &lt;= m &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= arrays[i].length &lt;= 500</code></li>
	<li><code>-10<sup>4</sup> &lt;= arrays[i][j] &lt;= 10<sup>4</sup></code></li>
	<li><code>arrays[i]</code> is sorted in <strong>ascending order</strong>.</li>
	<li>There will be at most <code>10<sup>5</sup></code> integers in all the arrays.</li>
</ul>

class Solution {
public:
    int maxDistance(vector<vector<int>>& mat) {
       int mn=mat[0][0];
       int mx=mat[0][mat[0].size()-1];
       int n=mat.size(),max_distance=0;
       for(int i=1;i<n;i++) {
            vector<int> vec=mat[i];
            max_distance=max(max_distance,abs(vec[0]-mx));
            max_distance=max(max_distance,abs(vec.back()-mn));
            mn=min(mn,vec[0]);
            mx=max(mx,vec.back());
       }
       /* priority_queue<int,vector<int>,greater<int>> pq;
        sort(mat.begin(),mat.end());
        int mi=abs(mat[0][0]-mat[1][0]);
        pq.push(mat[0][0]);
        for(int i=1;i<mat.size();i++) {
            if(!pq.empty() && mat[i][mat[i].size()-1]>=pq.top()) {
                mi=max(mi,abs(mat[i][mat[i].size()-1]-pq.top()));
                pq.pop();
            }
            pq.push(mat[i][0]);
        }*/
        // vector<int> dp1,dp2;
        /*for(int i=0;i<mat.size();i++) {
            dp1.push_back(mat[i][0]);
            dp2.push_back(mat[i][mat[i].size()-1]);
        }*/
        /*int mn=*min_element(dp1.begin(),dp1.end());
        int mx=*max_element(dp2.begin(),dp2.end());*/
        /*for(int i=1;i<mat.size();i++) {
            vector<int> dp1=mat[i-1];
            vector<int> dp2=mat[i];
            mi=max(mi,abs(dp1[0]-dp2[dp2.size()-1]));
        }
        sort(mat.begin(),mat.end());
        for(int i=1;i<mat.size();i++) {
            vector<int> dp1=mat[i-1];
            vector<int> dp2=mat[i];
            mi=max(mi,abs(dp1[0]-dp2[dp2.size()-1]));
        }*/
        /*for(int i=0;i<mat.size();i++) {
            vector<int> dp1=mat[i];
            for(int j=i+1;j<mat.size();j++) {
                vector<int> dp2=mat[j];
                mi=max(mi,abs(dp1[0]-dp2[dp2.size()-1]));
            }
        }
        sort(mat.begin(),mat.end());
        for(int i=0;i<mat.size();i++) {
            vector<int> dp1=mat[i];
            for(int j=i+1;j<mat.size();j++) {
                vector<int> dp2=mat[j];
                mi=max(mi,abs(dp1[0]-dp2[dp2.size()-1]));
            }
        }*/
        return max_distance;
    }
};
121261