← DiffPush

Convert Min Heap to Max Heap

Baseline Heaps · Learning O(n) · O(1)

The Ticket Queue Direction Flip

A support desk's ticket array is currently organized so the front holds the least urgent ticket (a min arrangement). Management wants the exact same array positions reorganized so the front holds the most urgent one (a max arrangement), in place. Since the input already satisfies the complete-tree shape, a bottom-up pass of downward fixes flips the polarity without touching the array's size.

Input: An integer array arr representing a valid min heap of size n.

Output: Return the array reorganized into a valid max heap.

Constraints

Examples

Example 1

Input: {"arr":[3,5,6,9,12]}
Output: [12,9,6,3,5]
Sifting down from the last internal node lifts 12 to the root and settles the rest into max order.

Example 2

Input: {"arr":[1,3,2,7,6,5,4]}
Output: [7,6,5,3,1,2,4]
The Floyd build pass rearranges every subtree bottom-up.

Solve this in your browser →

Also on LeetCode ↗