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

// #include <cstdlib>
#include <algorithm>
#include <map>

class Solution {
public:
    int maxDistance(vector<vector<int>>& arrays) {
        // int dis = 0;    // the max distance to return
        // int a, b, temp;
        // for(int i = 0; i < arrays.size(); i++) {
        //     b = arrays[i].front();    // the min element in arrays[i]
        //     for (int j = 0; j < arrays.size(); j++) {
        //         if (j != i) {
        //             a = arrays[j].back();   // the max element in arrays[j]
        //             if (abs(a - b) > dis) dis = abs(a - b);
        //         }
        //     }
        // }
        // return dis;

        int min1, max1;
        map<int, vector<int>> min;  // {element, index in arrays}   { {-3, {1}}, {-1, {0}} }
        map<int, vector<int>> max;  // {element, index in arrays}   { {1, {0}}, {4, {1}} }
        min[arrays[0].front()] = {0};
        max[arrays[0].back()] = {0};
        if (arrays[1].front() != arrays[0].front()) min[arrays[1].front()] = {1};
        else min[arrays[0].front()].push_back(1);
        if (arrays[1].back() != arrays[0].back()) max[arrays[1].back()] = {1};
        else max[arrays[0].back()].push_back(1);
        min1 = min.begin()->first;   // min1 = -3
        max1 = max.rbegin()->first;  // max1 = 4

        // update the min/max elements among arrays
        for (int i = 2; i < arrays.size(); i++) {
            // update min1
            if (arrays[i].front() < min1) {
                min1 = arrays[i].front();
                min[min1] = {i};
            }
            else {
                min[arrays[i].front()].push_back(i);
            }

            // update max1
            if (arrays[i].back() > max1) {
                max1 = arrays[i].back();
                max[max1] = {i};
            }
            else {
                max[arrays[i].back()].push_back(i);
            }
        }

        if (min1 == max1) return 0;
        if (min[min1].size() == 1 && max[max1].size() == 1 && 
            min[min1].front() == max[max1].front()) {
                // min1 and max1 are at the same array, need to compare |max2 - min1| and |max1 - min2|
                return (next(max.rbegin(), 1)->first - min1) > (max1 - next(min.begin(), 1)->first) ?
                    next(max.rbegin(), 1)->first - min1 :
                    max1 - next(min.begin(), 1)->first;
            }
        else return max1 - min1;
    }
};
116791