<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:
    def maxDistance(self, arrays: List[List[int]]) -> int:

        minimum1, minimum2 = arrays[0][0], arrays[1][0]
        maximum1, maximum2 = arrays[0][-1], arrays[1][-1]

        arrLen = len(arrays)

        if arrLen == 2:
            return max(maximum2 - minimum1, maximum1 - minimum2)

        smallest, largest = [], []
        for i, array in enumerate(arrays):
            smallest.append((array[0],i))
            largest.append((array[-1], i))

        smallest.sort()
        largest.sort(reverse = True)

        #print(smallest)
        #print(largest)

        if smallest[0][1] != largest[0][1]:
            return largest[0][0] - smallest[0][0]
        else:
            return max(largest[0][0] - smallest[1][0], largest[1][0] - smallest[0][0])

#        l = 0
#        for r in range(smallest):
#            if smallest[r][1] != largest[r][1]:
#                return 

#        arrays.sort()
#        largestValues = sorted(arrays, key=lambda x: x[-1], reverse = True)

#        print(arrays)
#        print(largestValues)

#        minMax = []
#        for array in arrays:
#            minMax.append((array[0], array[-1]))

#        print(minMax)

        return -1
47159