forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmallestrange.py
62 lines (44 loc) · 1.78 KB
/
smallestrange.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
smallest_range function takes a list of sorted integer lists and
finds the smallest range that includes at least one number from each list,
using a min heap for efficiency.
"""
import doctest
import heapq
import sys
def smallest_range(nums: list[list[int]]) -> list[int]:
"""
Find the smallest range from each list in nums.
Uses min heap for efficiency. The range includes at least one number from each list.
Args:
nums (list of list of int): List of k sorted integer lists.
Returns:
list: Smallest range as a two-element list.
Examples:
>>> smallest_range([[4,10,15,24,26], [0,9,12,20], [5,18,22,30]])
[20, 24]
>>> smallest_range([[1,2,3], [1,2,3], [1,2,3]])
[1, 1]
"""
min_heap: list[tuple[int, int, int]] = []
current_max = -sys.maxsize - 1
for i, list_ in enumerate(nums):
heapq.heappush(min_heap, (list_[0], i, 0))
current_max = max(current_max, list_[0])
# Initialize smallest_range with large integer values
smallest_range = [-sys.maxsize - 1, sys.maxsize]
while min_heap:
current_min, list_index, element_index = heapq.heappop(min_heap)
if current_max - current_min < smallest_range[1] - smallest_range[0]:
smallest_range = [current_min, current_max]
if element_index == len(nums[list_index]) - 1:
break
next_element = nums[list_index][element_index + 1]
heapq.heappush(min_heap, (next_element, list_index, element_index + 1))
current_max = max(current_max, next_element)
return smallest_range
if __name__ == "__main__":
doctest.testmod()
example1 = [[4, 10, 15, 24, 26], [0, 9, 12, 20], [5, 18, 22, 30]]
output1 = smallest_range(example1)
print("smallest range:", output1) # Output: [20, 24]