Relative Content

Tag Archive for python-3.xheap

Why heapq.heappush is an O(log n) operation?

import heapq minHeap = [4, 7, 2, 8, 1, 3] heapq.heapify(minHeap) # O(n log n) operation print(minHeap) # [1, 4, 2, 8, 7, 3] heapq.heappush(minHeap, 1) # O(log n) operation? print(minHeap) # [1, 4, 1, 8, 7, 3, 2] I guess heapq.heappush will create a new array and assigned to minHeap. If this is correct, […]

Why heapq.heappush is an O(log n) operation?

import heapq minHeap = [4, 7, 2, 8, 1, 3] heapq.heapify(minHeap) # O(n log n) operation print(minHeap) # [1, 4, 2, 8, 7, 3] heapq.heappush(minHeap, 1) # O(log n) operation? print(minHeap) # [1, 4, 1, 8, 7, 3, 2] I guess heapq.heappush will create a new array and assigned to minHeap. If this is correct, […]

Using Python heapq.heapify(Array)

Python: I want to use heapq.heapify(Arr) but everytime i want a slice of array to be used without creating new memory allocation for the new array . I want the rest of the array to be as is. example: heapq.heapify(arr[0:3]) and use rest of arr[3::] in some calculation and go on changing this slice width in a loop . Can I do it? I dont see arr being heapified in this case