From 15810110b90f0f1a2c2c646c4fb312424436e956 Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Sat, 25 Nov 2023 20:03:46 -0500 Subject: [PATCH 01/16] Create smallestRange.py --- greedy_methods/smallestRange.py | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 greedy_methods/smallestRange.py diff --git a/greedy_methods/smallestRange.py b/greedy_methods/smallestRange.py new file mode 100644 index 000000000000..db6586a95bc8 --- /dev/null +++ b/greedy_methods/smallestRange.py @@ -0,0 +1,59 @@ +import doctest +import heapq + +def smallest_range(nums): + """ + Finds the smallest range that includes at least one number from each of the k lists. + + The function uses a min heap to efficiently find the smallest range. + Each time, it pops the smallest element from the heap and adds the next element + from the same list to maintain the invariant of having one element from each list in the heap. + + Args: + nums (list of list of int): A list of k lists of sorted integers in non-decreasing order. + + Returns: + list: A list containing two integers representing the smallest range. + + 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] + """ + + # Create a min heap to store the first element from each list + min_heap = [] + current_max = float('-inf') + + # Initialize the heap with the first element from each list + for i, list_ in enumerate(nums): + heapq.heappush(min_heap, (list_[0], i, 0)) + current_max = max(current_max, list_[0]) + + # Initialize the smallest range as the largest possible + smallest_range = [float('-inf'), float('inf')] + + while min_heap: + current_min, list_index, element_index = heapq.heappop(min_heap) + + # Update the smallest range if the current range is smaller + if current_max - current_min < smallest_range[1] - smallest_range[0]: + smallest_range = [current_min, current_max] + + # Break if rreaching the end of one of the lists + if element_index == len(nums[list_index]) - 1: + break + + # Add the next element from the same list to the heap + 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) # Smallest Range: [20, 24] From d1fc4bfb4ed70ade8e2fe484a7a36c3c7c6853ec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 26 Nov 2023 01:09:50 +0000 Subject: [PATCH 02/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/smallestRange.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/greedy_methods/smallestRange.py b/greedy_methods/smallestRange.py index db6586a95bc8..6be2aaa681d2 100644 --- a/greedy_methods/smallestRange.py +++ b/greedy_methods/smallestRange.py @@ -1,6 +1,7 @@ import doctest import heapq + def smallest_range(nums): """ Finds the smallest range that includes at least one number from each of the k lists. @@ -24,7 +25,7 @@ def smallest_range(nums): # Create a min heap to store the first element from each list min_heap = [] - current_max = float('-inf') + current_max = float("-inf") # Initialize the heap with the first element from each list for i, list_ in enumerate(nums): @@ -32,7 +33,7 @@ def smallest_range(nums): current_max = max(current_max, list_[0]) # Initialize the smallest range as the largest possible - smallest_range = [float('-inf'), float('inf')] + smallest_range = [float("-inf"), float("inf")] while min_heap: current_min, list_index, element_index = heapq.heappop(min_heap) @@ -52,8 +53,9 @@ def smallest_range(nums): return smallest_range + if __name__ == "__main__": doctest.testmod() - example1 = [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] + example1 = [[4, 10, 15, 24, 26], [0, 9, 12, 20], [5, 18, 22, 30]] output1 = smallest_range(example1) print("Smallest Range:", output1) # Smallest Range: [20, 24] From 920de41d5137a631f9fdf7f05be250b879cc0d50 Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Sat, 25 Nov 2023 20:13:37 -0500 Subject: [PATCH 03/16] Update smallestRange.py --- greedy_methods/smallestRange.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/greedy_methods/smallestRange.py b/greedy_methods/smallestRange.py index 6be2aaa681d2..463a596f2d18 100644 --- a/greedy_methods/smallestRange.py +++ b/greedy_methods/smallestRange.py @@ -1,3 +1,7 @@ +""" +https://medium.com/@saiashish3760/leetcode-q632-smallest-range-covering-elements-from-k-lists-q507-496c21157914 +""" + import doctest import heapq From f7b3c7cea42a196260825a037e95f1e50e8914b4 Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Sat, 25 Nov 2023 20:15:25 -0500 Subject: [PATCH 04/16] Update smallestRange.py --- greedy_methods/smallestRange.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/greedy_methods/smallestRange.py b/greedy_methods/smallestRange.py index 463a596f2d18..b19624b5028c 100644 --- a/greedy_methods/smallestRange.py +++ b/greedy_methods/smallestRange.py @@ -8,14 +8,17 @@ def smallest_range(nums): """ - Finds the smallest range that includes at least one number from each of the k lists. + Finds the smallest range that includes at least one number + from each of the k lists. The function uses a min heap to efficiently find the smallest range. - Each time, it pops the smallest element from the heap and adds the next element - from the same list to maintain the invariant of having one element from each list in the heap. + Each time, it pops the smallest element from the + heap and adds the next element from the same list to + maintain the invariant of having one element from each list in the heap. Args: - nums (list of list of int): A list of k lists of sorted integers in non-decreasing order. + nums (list of list of int): A list of k lists of sorted integers + in non-decreasing order. Returns: list: A list containing two integers representing the smallest range. From 63a111f9dfcdf390dc1608ba97beb1d67e11e2ef Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 26 Nov 2023 01:16:01 +0000 Subject: [PATCH 05/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/smallestRange.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/greedy_methods/smallestRange.py b/greedy_methods/smallestRange.py index b19624b5028c..6624b88e0bb9 100644 --- a/greedy_methods/smallestRange.py +++ b/greedy_methods/smallestRange.py @@ -8,16 +8,16 @@ def smallest_range(nums): """ - Finds the smallest range that includes at least one number + Finds the smallest range that includes at least one number from each of the k lists. The function uses a min heap to efficiently find the smallest range. - Each time, it pops the smallest element from the - heap and adds the next element from the same list to + Each time, it pops the smallest element from the + heap and adds the next element from the same list to maintain the invariant of having one element from each list in the heap. Args: - nums (list of list of int): A list of k lists of sorted integers + nums (list of list of int): A list of k lists of sorted integers in non-decreasing order. Returns: From b0ab04082fae1e13c5129087c75744ba59b56cd7 Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Sat, 25 Nov 2023 20:28:02 -0500 Subject: [PATCH 06/16] Update smallestRange.py --- greedy_methods/smallestRange.py | 38 +++++++++++---------------------- 1 file changed, 13 insertions(+), 25 deletions(-) diff --git a/greedy_methods/smallestRange.py b/greedy_methods/smallestRange.py index 6624b88e0bb9..fa97e2bb82c3 100644 --- a/greedy_methods/smallestRange.py +++ b/greedy_methods/smallestRange.py @@ -1,27 +1,21 @@ """ https://medium.com/@saiashish3760/leetcode-q632-smallest-range-covering-elements-from-k-lists-q507-496c21157914 """ - import doctest import heapq +import sys - -def smallest_range(nums): +def smallest_range(nums: list[list[int]]) -> list[int]: """ - Finds the smallest range that includes at least one number - from each of the k lists. - - The function uses a min heap to efficiently find the smallest range. - Each time, it pops the smallest element from the - heap and adds the next element from the same list to - maintain the invariant of having one element from each list in the heap. + 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): A list of k lists of sorted integers - in non-decreasing order. + nums (list of list of int): List of k sorted integer lists. Returns: - list: A list containing two integers representing the smallest range. + list: Smallest range as a two-element list. Examples: >>> smallest_range([[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]) @@ -30,39 +24,33 @@ def smallest_range(nums): [1, 1] """ - # Create a min heap to store the first element from each list - min_heap = [] - current_max = float("-inf") + min_heap: list[tuple[int, int, int]] = [] + current_max = -sys.maxsize - 1 - # Initialize the heap with the first element from each list for i, list_ in enumerate(nums): heapq.heappush(min_heap, (list_[0], i, 0)) current_max = max(current_max, list_[0]) - # Initialize the smallest range as the largest possible - smallest_range = [float("-inf"), float("inf")] + # 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) - # Update the smallest range if the current range is smaller if current_max - current_min < smallest_range[1] - smallest_range[0]: smallest_range = [current_min, current_max] - # Break if rreaching the end of one of the lists if element_index == len(nums[list_index]) - 1: break - # Add the next element from the same list to the heap 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]] + example1 = [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] output1 = smallest_range(example1) - print("Smallest Range:", output1) # Smallest Range: [20, 24] + print("Smallest Range:", output1) # Output: [20, 24] From 5a9fcdd11ddc4348d82454584dcb1e18ef0188a8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 26 Nov 2023 01:28:37 +0000 Subject: [PATCH 07/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/smallestRange.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/greedy_methods/smallestRange.py b/greedy_methods/smallestRange.py index fa97e2bb82c3..96480efba816 100644 --- a/greedy_methods/smallestRange.py +++ b/greedy_methods/smallestRange.py @@ -5,10 +5,11 @@ 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: @@ -49,8 +50,9 @@ def smallest_range(nums: list[list[int]]) -> list[int]: return smallest_range + if __name__ == "__main__": doctest.testmod() - example1 = [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]] + 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] From dec45ffd0b57c31a84149a3d0445801550c4e94b Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Sat, 25 Nov 2023 20:31:14 -0500 Subject: [PATCH 08/16] Update and rename smallestRange.py to smallestrange.py --- greedy_methods/{smallestRange.py => smallestrange.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename greedy_methods/{smallestRange.py => smallestrange.py} (96%) diff --git a/greedy_methods/smallestRange.py b/greedy_methods/smallestrange.py similarity index 96% rename from greedy_methods/smallestRange.py rename to greedy_methods/smallestrange.py index 96480efba816..4919094aae17 100644 --- a/greedy_methods/smallestRange.py +++ b/greedy_methods/smallestrange.py @@ -55,4 +55,4 @@ def smallest_range(nums: list[list[int]]) -> list[int]: 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] + print("smallest range:", output1) # Output: [20, 24] From c2fdc3f6f75715159c204f1f091b01bdf62c45a9 Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Wed, 29 Nov 2023 18:02:14 -0500 Subject: [PATCH 09/16] Update smallestrange.py --- greedy_methods/smallestrange.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/greedy_methods/smallestrange.py b/greedy_methods/smallestrange.py index 4919094aae17..678b9fe4f30d 100644 --- a/greedy_methods/smallestrange.py +++ b/greedy_methods/smallestrange.py @@ -1,6 +1,3 @@ -""" -https://medium.com/@saiashish3760/leetcode-q632-smallest-range-covering-elements-from-k-lists-q507-496c21157914 -""" import doctest import heapq import sys From 961c2f1ed96d6a48e159bf7e41cdd661adf27de0 Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Thu, 30 Nov 2023 08:37:51 -0500 Subject: [PATCH 10/16] Update smallestrange.py --- greedy_methods/smallestrange.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/greedy_methods/smallestrange.py b/greedy_methods/smallestrange.py index 678b9fe4f30d..01b75cdab757 100644 --- a/greedy_methods/smallestrange.py +++ b/greedy_methods/smallestrange.py @@ -1,3 +1,10 @@ +""" + 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 From 104037c7ed34ee59cde6551d53eb3ad24f133b0d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 30 Nov 2023 13:39:50 +0000 Subject: [PATCH 11/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/smallestrange.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_methods/smallestrange.py b/greedy_methods/smallestrange.py index 01b75cdab757..4109e3c13af0 100644 --- a/greedy_methods/smallestrange.py +++ b/greedy_methods/smallestrange.py @@ -1,6 +1,6 @@ """ smallest_range function takes a list of sorted integer lists and - finds the smallest range that includes at least one number from each list, + finds the smallest range that includes at least one number from each list, using a min heap for efficiency. """ From 7901221b928f0816af5c8b99f60ae192a92e6487 Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Thu, 30 Nov 2023 08:43:16 -0500 Subject: [PATCH 12/16] Update smallestrange.py --- greedy_methods/smallestrange.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/greedy_methods/smallestrange.py b/greedy_methods/smallestrange.py index 4109e3c13af0..c579b55501d5 100644 --- a/greedy_methods/smallestrange.py +++ b/greedy_methods/smallestrange.py @@ -1,10 +1,9 @@ """ - 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. +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 From 06f64b7a6b5eabedd74056fdf88861d0200458c2 Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Thu, 30 Nov 2023 08:55:12 -0500 Subject: [PATCH 13/16] Rename smallestrange.py to smallest_range.py --- greedy_methods/{smallestrange.py => smallest_range.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename greedy_methods/{smallestrange.py => smallest_range.py} (100%) diff --git a/greedy_methods/smallestrange.py b/greedy_methods/smallest_range.py similarity index 100% rename from greedy_methods/smallestrange.py rename to greedy_methods/smallest_range.py From 8ac97c9831916b59bf8c4c8ff169d4c0c083042f Mon Sep 17 00:00:00 2001 From: Pedram_Mohajer <48964282+pedram-mohajer@users.noreply.github.com> Date: Fri, 1 Dec 2023 10:47:59 -0500 Subject: [PATCH 14/16] Update smallest_range.py From 5f29c6b2598c52e5d2ebeb43eefde0877a68da4b Mon Sep 17 00:00:00 2001 From: Christian Clauss <cclauss@me.com> Date: Fri, 1 Dec 2023 17:21:23 +0100 Subject: [PATCH 15/16] Update smallest_range.py --- greedy_methods/smallest_range.py | 40 +++++++++++++++----------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/greedy_methods/smallest_range.py b/greedy_methods/smallest_range.py index c579b55501d5..eeb47b0cb322 100644 --- a/greedy_methods/smallest_range.py +++ b/greedy_methods/smallest_range.py @@ -1,12 +1,10 @@ """ -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. +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 +from heapq import heappop, heappush +from sys import maxsize def smallest_range(nums: list[list[int]]) -> list[int]: @@ -16,30 +14,30 @@ def smallest_range(nums: list[list[int]]) -> list[int]: 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. + nums: List of k sorted integer lists. Returns: - list: Smallest range as a two-element list. + Smallest range as a two-element list. Examples: - >>> smallest_range([[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]) + >>> 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]]) + >>> 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 + current_max = -maxsize - 1 - for i, list_ in enumerate(nums): - heapq.heappush(min_heap, (list_[0], i, 0)) - current_max = max(current_max, list_[0]) + for i, items in enumerate(nums): + heappush(min_heap, (items[0], i, 0)) + current_max = max(current_max, items[0]) # Initialize smallest_range with large integer values - smallest_range = [-sys.maxsize - 1, sys.maxsize] + smallest_range = [-maxsize - 1, maxsize] while min_heap: - current_min, list_index, element_index = heapq.heappop(min_heap) + current_min, list_index, element_index = heappop(min_heap) if current_max - current_min < smallest_range[1] - smallest_range[0]: smallest_range = [current_min, current_max] @@ -48,14 +46,14 @@ def smallest_range(nums: list[list[int]]) -> list[int]: break next_element = nums[list_index][element_index + 1] - heapq.heappush(min_heap, (next_element, list_index, element_index + 1)) + 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] + from doctest import testmod + + testmod() + print(f"{smallest_range([[1, 2, 3], [1, 2, 3], [1, 2, 3]])}") # Output: [1, 1] From 4cf8821aca59dfbc53bf727dcca62083244b4d67 Mon Sep 17 00:00:00 2001 From: Christian Clauss <cclauss@me.com> Date: Fri, 1 Dec 2023 17:51:06 +0100 Subject: [PATCH 16/16] Update smallest_range.py --- greedy_methods/smallest_range.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/greedy_methods/smallest_range.py b/greedy_methods/smallest_range.py index eeb47b0cb322..e2b7f8d7e96a 100644 --- a/greedy_methods/smallest_range.py +++ b/greedy_methods/smallest_range.py @@ -17,13 +17,25 @@ def smallest_range(nums: list[list[int]]) -> list[int]: nums: List of k sorted integer lists. Returns: - Smallest range as a two-element list. + 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] + >>> smallest_range(((1, 2, 3), (1, 2, 3), (1, 2, 3))) + [1, 1] + >>> smallest_range(((-3, -2, -1), (0, 0, 0), (1, 2, 3))) + [-1, 1] + >>> smallest_range([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + [3, 7] + >>> smallest_range([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) + [0, 0] + >>> smallest_range([[], [], []]) + Traceback (most recent call last): + ... + IndexError: list index out of range """ min_heap: list[tuple[int, int, int]] = []