1import bintrees, math
2class Solution:
3 def findClosestEntriesIn3SortedArrays(self, sorted_arrays):
4 iters = bintrees.RBTree()
5 for i, sorted_array in enumerate(sorted_arrays):
6 it = iter(sorted_array)
7 firstMin = next(sorted_array, None)
8 if firstMin: iters.insert((firstMin, i), it)
9
10 res = math.inf
11 while True:
12 minVal, minIndex = iters.min_key()
13 maxVal, _ = iters.max_key()
14 res = min(res, maxVal - minVal)
15 it = iters.pop_min()[1]
16 nextMin = next(it, None)
17 if not nextMin: return res
18 iters.insert((nextMin, minIndex), it)
19
20 return res