Maximum Distance in Arrays
Input:
[ [1,2,3],
[4,5],
[1,2,3] ]
Output: 4
Explanation:
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.思路
Python code:
class Solution:
def maxDistance(self, arrays):
"""
:type arrays: List[List[int]]
:rtype: int
"""
currMax = -10001
currMin = 10001
res = 0
for lst in arrays:
if lst:
res = max(res, max(currMax - lst[0], lst[-1] - currMin))
currMax, currMin = max(currMax, lst[-1]), min(currMin, lst[0])
return res