oneby7

Heaps Building a heap

401. Implement Max Heap

Medium

Build a max heap of integers on an array. A max heap gives back its largest value first, whatever order the values went in. Write a class MaxHeap with four methods:

  • push(x) adds x to the heap.
  • pop() removes the largest value and returns it.
  • peek() returns the largest value without removing it.
  • size() returns how many values the heap holds.

push and pop must run in O(log n) time, and peek and size in O(1), where n is the number of values in the heap.

Keep the values in an array (a list or a vector) and arrange them yourself. Your language's priority queue (priority_queue, PriorityQueue, heapq, BinaryHeap) would pass the tests too, because the judge only sees what your methods return. Building the heap yourself is the exercise.

Your class is tested by replaying a list of calls on a new, empty MaxHeap. ops[i] names the i-th call: "push", "pop", "peek" or "size". The pushes take their values from values in order, so the first push pushes values[0], the second pushes values[1], and so on. The answer is the list of every value that pop, peek and size return, in the order they are called.

In C# and Go the methods are Push, Pop, Peek and Size. Go creates the heap with Constructor(), and Rust with MaxHeap::new(). The names in ops are the same in every language.

Example 1

Input: ops = ["push", "push", "push", "peek", "pop", "size", "push", "pop", "pop"] values = [4, 9, 2, 7]

Output: [9,9,2,7,4]

Explanation: After three pushes the heap holds 4, 9 and 2. peek and pop both give 9, and pop removes it, so two values are left. Then 7 arrives, and it is larger than 4 and 2, so the next pop gives 7 and the last gives 4.

Example 2

Input: ops = ["push", "push", "push", "push", "pop", "pop", "peek", "size"] values = [-3, -8, -3, -1]

Output: [-1,-3,-3,2]

Explanation: -1 is the largest of these negative values, so it comes out first. -3 is in the heap twice, and the second pop removes only one copy, so peek still gives -3.

Example 3

Input: ops = ["size", "push", "push", "pop", "size", "peek"] values = [-1000000000, 1000000000]

Output: [0,1000000000,1,-1000000000]

Explanation: A new heap holds no values, so size is 0. After 1000000000 is popped, -1000000000 is the only value left.

Check your understanding

Work out the answer for this input before you write any code.

ops = ["push", "push", "push", "pop", "pop"] values = [5, 2, 7]

Constraints

  • 1 ≤ ops.length ≤ 105
  • Each ops[i] is "push", "pop", "peek" or "size".
  • values.length equals the number of "push" calls in ops.
  • -109 ≤ values[i] ≤ 109
  • pop and peek are only called when the heap is not empty.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.