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

    struct Tuple {
       int idx;
       int num;
       Tuple(int i,int n) {idx=i, num=n;}
    };

class Solution {
    deque<struct Tuple> minque;
    deque<struct Tuple> maxque;

public:
    void addMin(int idx, int num)
    {
        int sz = minque.size();
        if(sz == 0) {
            minque.push_back(Tuple(idx,num));
        }
        else if(sz == 1) {
            if(num < minque.front().num)
                minque.push_front(Tuple(idx,num));
            else
                minque.push_back(Tuple(idx,num));
        }
        else {
            if(num < minque.front().num) {
                minque.push_front(Tuple(idx,num));
                minque.pop_back();
            }
            else if(num < minque.back().num) {
                minque.pop_back();
                minque.push_back(Tuple(idx,num));
            }
        }
    }
    void addMax(int idx, int num)
    {
        int sz = maxque.size();
        if(sz == 0) {
            maxque.push_back(Tuple(idx,num));
        }
        else if(sz == 1) {
            if(num > maxque.front().num)
                maxque.push_front(Tuple(idx,num));
            else
                maxque.push_back(Tuple(idx,num));
        }
        else {
            if(num > maxque.front().num) {
                maxque.push_front(Tuple(idx,num));
                maxque.pop_back();
            }
            else if(num > maxque.back().num) {
                maxque.pop_back();
                maxque.push_back(Tuple(idx,num));
            }
        }
    }
    int max(int x, int y) {
        if (x > y)
            return x;
        return y;
    }
    int maxDistance(vector<vector<int>>& arrays) {
        //maintain top two min with arr index
        //maintain top two max with arr index
        for(int i=0; i<arrays.size(); i++) {
            vector<int> &arr = arrays[i];
            addMin(i, arr[0]);
            addMax(i, arr[arr.size()-1]);
        }
        //choose min and max so that it doesnt clash
        if(maxque.front().idx != minque.front().idx) {
            return maxque.front().num - minque.front().num;
        }
        return max( maxque.front().num - minque.back().num,
            maxque.back().num - minque.front().num);
    }
};
109341