← 返回 bytedance 的题目列表Implement Min Heap
类型:online_judge
Implement a Min Heap using an array to maintain the structure of a binary tree. Key operations to implement:
push: Insert an element into the heap while maintaining the heap property.
pop: Remove and return the smallest element from the heap while maintaining the heap property.
For a node at position i, its children are at positions 2i + 1 and 2i + 2. Optimize these operations for efficient time complexity. Use the input/output format and test cases below:
Input:
insert: a positive integer
remove: indicates removing and returning the minimum value from the heap
print_heap: outputs the current heap
Sample Input:
insert 3
insert 1
insert 2
remove
print_heap
Sample Output:
1
2 3
Example
Input
insert 10
insert 5
insert 20
remove
print_heap