Complete the code to insert an element into a min heap using heapq.
import heapq heap = [] heapq.[1](heap, 5) print(heap)
The heappush function adds an element to the heap while maintaining the heap property.
Complete the code to get the smallest element from a min heap.
import heapq heap = [3, 5, 7] heapq.heapify(heap) smallest = heap[1] print(smallest)
In a min heap, the smallest element is always at index 0.
Fix the error in the code to create a max heap using heapq.
import heapq nums = [1, 3, 5] max_heap = [-n for n in nums] heapq.[1](max_heap) print([-n for n in max_heap])
To convert a list into a heap, use heapify. This is needed after negating values for max heap.
Fill both blanks to create a max heap and pop the largest element.
import heapq nums = [2, 4, 6] max_heap = [[1] for n in nums] heapq.[2](max_heap) largest = -heapq.heappop(max_heap) print(largest)
Negate numbers to simulate max heap and use heapify to build the heap.
Fill all three blanks to create a min heap, add an element, and pop the smallest.
import heapq heap = [10, 20, 30] heapq.[1](heap) heapq.[2](heap, [3]) smallest = heapq.heappop(heap) print(smallest)
First build the heap with heapify, then add 5 with heappush, and pop the smallest element.
