<p>You have <code>k</code> lists of sorted integers in <strong>non-decreasing&nbsp;order</strong>. Find the <b>smallest</b> range that includes at least one number from each of the <code>k</code> lists.</p>

<p>We define the range <code>[a, b]</code> is smaller than range <code>[c, d]</code> if <code>b - a &lt; d - c</code> <strong>or</strong> <code>a &lt; c</code> if <code>b - a == d - c</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
<strong>Output:</strong> [20,24]
<strong>Explanation: </strong>
List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
List 2: [0, 9, 12, 20], 20 is in range [20,24].
List 3: [5, 18, 22, 30], 22 is in range [20,24].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [[1,2,3],[1,2,3],[1,2,3]]
<strong>Output:</strong> [1,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>nums.length == k</code></li>
	<li><code>1 &lt;= k &lt;= 3500</code></li>
	<li><code>1 &lt;= nums[i].length &lt;= 50</code></li>
	<li><code>-10<sup>5</sup> &lt;= nums[i][j] &lt;= 10<sup>5</sup></code></li>
	<li><code>nums[i]</code>&nbsp;is sorted in <strong>non-decreasing</strong> order.</li>
</ul>

class Solution:
    def smallestRange(self, nums: List[List[int]]) -> List[int]:
        count = defaultdict(set)
        for i, num in enumerate(nums):
            for n in num:
                count[n].add(i)
        keys = sorted(count.keys())
        result = [min(num[0] for num in nums), max(num[-1] for num in nums)]
        left = 0
        window = defaultdict(lambda : 0)
        for right in range(len(keys)):
            for group in count[keys[right]]:
                window[group] += 1
            while len(window) == len(nums):
                if keys[right] - keys[left] < result[1] - result[0]:
                    result = [keys[left], keys[right]]
                for group in count[keys[left]]:
                    window[group] -= 1
                    if window[group] == 0:
                        del window[group]
                left += 1
        return result





    ''' using heap, very long runtime... '''
#     def smallestRange(self, nums: List[List[int]]) -> List[int]:
#         heap = []
#         for i, num in enumerate(nums):
#             heap.extend((n, i) for n in num)
#         heapq.heapify(heap)
        
#         candidate = {}
#         result = [min(num[0] for num in nums), max(num[-1] for num in nums)]
#         while heap:
#             curr = heapq.heappop(heap)
#             if curr[1] in candidate:
#                 candidate[curr[1]] = curr[0]
#             else:
#                 candidate[curr[1]] = curr[0]
#             if len(candidate) == len(nums):
#                 max_val = max(candidate.values())
#                 min_val = min(candidate.values())
#                 if max_val - min_val < result[1] - result[0]:
#                     result = [min_val, max_val]
#         return result

25000