<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:
    bool isValid(vector<int>& idx1, vector<int>& idx2){
        if(idx1.size() == 0 || idx2.size() == 0) return false;
        if(idx1.size() > 1 || idx2.size() > 1) return true;
        return idx1[0] != idx2[0];
    }

    int maxDistance(vector<vector<int>>& arrays) {
        if(arrays.size() < 2) return 0;
        pair<int, vector<int>> min1(INT_MAX, vector<int>(0)), min2(INT_MAX, vector<int>(0));
        pair<int, vector<int>> max1(INT_MIN, vector<int>(0)), max2(INT_MIN, vector<int>(0));
        for(int i = 0;i < arrays.size();i++){
            int cur_min = arrays[i][0], cur_max = arrays[i].back();
            // min val
            if(cur_min < min1.first){
                min2 = min1;
                min1 = {cur_min, {i}};
            }else if(cur_min == min1.first){
                min1.second.push_back(i);
            }else if(cur_min < min2.first){
                min2 = {cur_min, {i}};
            }else if(cur_min == min2.first){
                min2.second.push_back(i);
            }
            // max val
            if(cur_max > max1.first){
                max2 = max1;
                max1 = {cur_max, {i}};
            }else if(cur_max == max1.first){
                max1.second.push_back(i);
            }else if(cur_max > max2.first){
                max2 = {cur_max, {i}};
            }else if(cur_max == max2.first){
                max2.second.push_back(i);
            }
        }
        // find the maximum distance
        int res = 0;
        if(isValid(min1.second, max1.second)) res = max(res, max1.first - min1.first);
        if(isValid(min1.second, max2.second)) res = max(res, max2.first - min1.first);
        if(isValid(min2.second, max1.second)) res = max(res, max1.first - min2.first);
        if(isValid(min2.second, max2.second)) res = max(res, max2.first - min2.first);
        return res;
    }
};
109528