-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsolution.py
30 lines (23 loc) · 1.04 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import heapq
class Solution:
def smallestRange(self, nums: List[List[int]]) -> List[int]:
minHeap = []
maxValue = float('-inf')
# Initialize heap with the first element from each list
for i in range(len(nums)):
heapq.heappush(minHeap, (nums[i][0], i, 0))
maxValue = max(maxValue, nums[i][0])
rangeStart, rangeEnd = 0, float('inf')
while minHeap:
minValue, row, col = heapq.heappop(minHeap)
# Update the smallest range
if maxValue - minValue < rangeEnd - rangeStart:
rangeStart, rangeEnd = minValue, maxValue
# Move to the next element in the current list
if col + 1 < len(nums[row]):
nextValue = nums[row][col + 1]
heapq.heappush(minHeap, (nextValue, row, col + 1))
maxValue = max(maxValue, nextValue)
else:
break # One list is exhausted
return [rangeStart, rangeEnd]