<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:
        dict = {}
        maxs = arrays[0][-1]
        mins = arrays[0][0]
        dict[arrays[0][-1]] = 0
        dict[arrays[0][0]] = 0
        for i in range(len(arrays)):
            if arrays[i][-1] > maxs:
                dict[arrays[i][-1]] = i
                maxs = arrays[i][-1]
            if arrays[i][0] < mins:
                dict[arrays[i][0]] = i
                mins = arrays[i][0]
        #print(maxs)
        #print(mins)
        print(dict)
        if dict[maxs] == dict[mins]:
            next_max = float('-inf')
            next_min = float('inf')
            for j in range(len(arrays)):
                if arrays[j][-1] == maxs:
                    if arrays[j][-1] == maxs and arrays[j][-1] in dict and j != dict[maxs]:
                            dict[arrays[j][-1]] = j
                            next_max = arrays[j][-1]
                elif arrays[j][-1] > next_max and arrays[j][-1] < maxs:
                    dict[arrays[j][-1]] = j
                    next_max = arrays[j][-1]

                if arrays[j][0] == mins:
                    if arrays[j][0] == mins and arrays[j][0] in dict and j != dict[mins]:
                        dict[arrays[j][0]] = j
                        next_min = arrays[j][0]

                elif arrays[j][0] < next_min and arrays[j][0] > mins:
                    dict[arrays[j][0]] = j
                    next_min = arrays[j][0]

            if next_max == float('-inf'):
                next_max = maxs
            if next_min == float('inf'):
                next_min = mins
                
            print(next_max)
            print(next_min)
            print(maxs)
            print(mins)
            




            if abs(maxs - next_max) > abs(mins - next_min):
                #print('n')
                return (maxs - next_min)
            elif abs(maxs - next_max) < abs(mins - next_min):
                #print("hi")
                return (next_max - mins)
            elif abs(maxs - next_max) == abs(mins - next_min):
                return (maxs - next_min)
        return (maxs - mins)


35784