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

        m = len(arrays)
        
        ## first run
        max_array1, min_array1 = arrays[0][-1], arrays[0][0]
        max_array2, min_array2 = arrays[1][-1], arrays[1][0]

        min_val = min(min_array1, min_array2)
        max_val = max(max_array1, max_array2)
        res_first = max(abs(max_array1 - min_array2), abs(max_array2 - min_array1))
        if m == 2:
            return res_first 

        for i in range(2, m):
            max_array, min_array = arrays[i][-1], arrays[i][0]
            res_first = max(res_first, abs(max_array - min_val), abs(min_array - max_val))
            min_val = min(min_array, min_val)
            max_val = max(max_array, max_val)

        ## second run 
        max_array1, min_array1 = arrays[-1][-1], arrays[-1][0]
        max_array2, min_array2 = arrays[-2][-1], arrays[-2][0]

        min_val = min(min_array1, min_array2)
        max_val = max(max_array1, max_array2)
        res_second = max(abs(max_array1 - min_array2), abs(max_array2 - min_array1))

        for i in range(m-3, -1, -1):
            max_array, min_array = arrays[i][-1], arrays[i][0]
            res_second = max(res_second, abs(max_array - min_val), abs(min_array - max_val))
            min_val = min(min_array, min_val)
            max_val = max(max_array, max_val)

        return max(res_first, res_second)
34034