<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>>& arrays) {
        int n = arrays.size();
        map<int, vector<int>> m1, m2;
        for(int i = 0; i < n; i++) {
            m1[arrays[i][0]].emplace_back(i);
            m2[arrays[i][arrays[i].size() - 1]].emplace_back(i);
        }
        map<int, vector<int>>:: iterator it1 = m1.begin();
        map<int, vector<int>>:: iterator it2 = m1.begin();
        it2++;
        map<int, vector<int>>:: reverse_iterator it3 = m2.rbegin();
        map<int, vector<int>>:: reverse_iterator it4 = m2.rbegin();
        it4++;
        while(true) {
            if(it1->second.size() > 1 || it3->second.size() > 1) 
                return (it3->first - it1->first);
            else if(it1->second[0] != it3->second[0])
                return (it3->first - it1->first);
            else
                break;
        }
        while(true) {
            int ans1 = 0, ans2 = 0;
            if(it4 != m2.rend()) {
                if(it1->second.size() > 1 || it4->second.size() > 1) 
                    ans1 = (it4->first - it1->first);
                else if(it1->second[0] != it4->second[0])
                    ans1 = abs(it4->first - it1->first);
            }
            if(it2 != m1.end()) {
                if(it2->second.size() > 1 || it3->second.size() > 1) 
                    ans1 = (it4->first - it1->first);
                else if(it2->second[0] != it3->second[0])
                    ans2 = abs(it2->first - it3->first);
            }
            return max(ans1, ans2);
        }
        while(true) {
            if(it2 != m1.end() && it4 != m2.rend()) {
                if(it2->second.size() > 1 || it4->second.size() > 1) 
                    return abs(it4->first - it2->first);
                else if(it2->second[0] != it4->second[0])
                    return abs(it4->first - it2->first);
                else
                    break;
            }
            else
                break;
        }
        return 0;
    }
};
116791