-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path!9. Nearly sorted - v6.py
More file actions
64 lines (52 loc) · 1.79 KB
/
!9. Nearly sorted - v6.py
File metadata and controls
64 lines (52 loc) · 1.79 KB
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
63
64
#User function Template for python3
class Solution:
#Function to return the sorted array.
def nearlySorted(self, arr, n, k):
# Initialize a min-heap
heap = [] # = tas en français
# Add first k+1 elements to the heap
for i in range(min(k+1, n)):
print(f'Heap push {arr[i]}')
heapq.heappush(heap, arr[i])
print(f'1 heap {heap}, array {arr}\n')
index = 0
# For the remaining elements in the array
for i in range(k+1, n):
# Extract the minimum element from the heap and place it in the array
print(f'Heap pop {heap} and add into array[{index}]')
arr[index] = heapq.heappop(heap)
index += 1
# Add the current element to the heap
print(f'Heap push {arr[i]}')
heapq.heappush(heap, arr[i])
print(f'2 heap {heap}, array {arr}\n')
# Extract remaining elements from the heap and place them in the array
while heap:
print(f'Heap pop {heap} and add into array[{index}]')
arr[index] = heapq.heappop(heap)
index += 1
print(f'3 heap {heap}, array {arr}')
return arr
#{
# Driver Code Starts
#Initial Template for Python 3
import atexit
import io
import sys
import heapq
from collections import defaultdict
# Contributed by : Nagendra Jha
if __name__ == '__main__':
test_cases = int(input())
for cases in range(test_cases) :
n,k = map(int,input().strip().split())
a = list(map(int,input().strip().split()))
ob=Solution()
print(*ob.nearlySorted(a,n,k))
# } Driver Code Ends
# Tested inputs :
'''
1
6 4
1 3 5 6 4 2
'''