← 返回 goldmansachs 的题目列表Implement Heap Insert
类型:online_judge
Write pseudocode or code for the add(value) operation of a binary min-heap.
The heap is stored in a zero-indexed array. add(value) must:
Append value to the end of the array;
Repeatedly swap it with its parent until the min-heap property is restored;
Return the final index of the inserted element.
State the time complexity and relevant edge cases.
Example
Input
5
5 3 8 1 4
Output
1 3 8 5 4