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

        '''
        Solution 1: O(mlogm)
        '''
        # maxheap = []
        # minheap = []

        # for i in range(len(arrays)):
        #     l,r = arrays[i][0], arrays[i][-1]
        #     heappush(maxheap, (-r, i))
        #     heappush(minheap, (l, i))

        # mx = heappop(maxheap)
        # mn = heappop(minheap)

        # if mx[1] != mn[1]:
        #     return -mx[0] - mn[0]

        # d1 = -maxheap[0][0] - mn[0]
        # d2 = -mx[0] - minheap[0][0]
        # return d1 if d1 > d2 else d2


        '''
        Solution 2: O(m)
        '''
        # topheap = []
        # bottomheap = []

        # for i in range(len(arrays)):
        #     l,r = arrays[i][0], arrays[i][-1]
        #     if len(topheap) < 2 or r > topheap[0][0]:
        #         heappush(topheap, (r, i))
        #         if len(topheap) > 2:
        #             heappop(topheap)
        #     if len(bottomheap) < 2 or l < -bottomheap[0][0]:
        #         heappush(bottomheap, (-l, i))
        #         if len(bottomheap) > 2:
        #             heappop(bottomheap)

        # mx1 = heappop(topheap)
        # mx2 = heappop(topheap)
        # mn1 = heappop(bottomheap)
        # mn2 = heappop(bottomheap)

        # if mx2[1] != mn2[1]:
        #     return mx2[0] + mn2[0]

        # return max(mx1[0] + mn2[0], mx2[0] + mn1[0])
        
        '''
        Solution 3:
        '''

        globalmin = arrays[0][0]
        globalmax = arrays[0][-1]
        rst = 0
        for i in range(1, len(arrays)):
            localmax = arrays[i][-1]
            localmin = arrays[i][0]
            rst = max(rst, max(globalmax - localmin, localmax - globalmin))
            globalmax = max(globalmax, localmax)
            globalmin = min(globalmin, localmin)

        return rst
38409