From 961010f38099aa9822e5fd89e445f46bfa09a7d4 Mon Sep 17 00:00:00 2001 From: Lyte above Nyte Date: Fri, 14 Feb 2025 07:37:43 +0330 Subject: [PATCH] FIX: adding upcoming elements to the heap, after first `k` ones. we've already added first k elements to the heap, so it's needed to read the sequence from k to the end and skip already added elements to avoid duplicate elements in result. --- epi_judge_python_solutions/sort_almost_sorted_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/epi_judge_python_solutions/sort_almost_sorted_array.py b/epi_judge_python_solutions/sort_almost_sorted_array.py index 52f6fa366..f5d7f887a 100644 --- a/epi_judge_python_solutions/sort_almost_sorted_array.py +++ b/epi_judge_python_solutions/sort_almost_sorted_array.py @@ -16,7 +16,7 @@ def sort_approximately_sorted_array(sequence: Iterator[int], result = [] # For every new element, add it to min_heap and extract the smallest. - for x in sequence: + for x in sequence[k:]: smallest = heapq.heappushpop(min_heap, x) result.append(smallest)