Complete the code to create an empty priority queue using a simple array.
const priorityQueue: number[] = [1];We use an empty array [] to start a priority queue as a list of numbers.
Complete the code to add a new element to the priority queue array.
priorityQueue.[1](5);
Use push to add an element to the end of the array, simulating adding to the priority queue.
Fix the error in the code to remove the highest priority element (smallest number) from the queue.
const highestPriority = priorityQueue.[1]();After sorting the queue so the smallest number is first, shift removes that first element.
Fill both blanks to sort the priority queue in ascending order before removing the highest priority element.
priorityQueue.[1]((a, b) => a [2] b);
Use sort with a compare function (a, b) => a - b to sort numbers ascending.
Fill all three blanks to create a function that inserts a number and keeps the priority queue sorted.
function insertAndSort(queue: number[], value: number) {
queue.[1](value);
queue.[2]((a, b) => a [3] b);
}First push adds the value, then sort with (a, b) => a - b keeps the queue sorted.
