Problems and solutions for LeetCode.
Several problems require access to a priority queue; however, TypeScript itself does not have such a built-in data structure.
LeetCode provides support for heaps via datastructures-js/priority-queue at version 5.4.0
. This is the library used, because this repository is for LeetCode submissions.
A couple of gotchas for version 5.4.0
that don't exist in later versions:
- LeetCode uses
require
style imports and notimport
. Practically that meansMaxPriorityQueue
andMinPriorityQueue
do not accept parameterized types. - Also,
MaxPriorityQueue
andMinPriorityQueue
are exported as values. To get the type, doInstanceType<typeof MaxPriorityQueue>
. - Primitives are wrapped. That is, if you do
heap.enqueue(1)
, then you shouldheap.dequeue().element
to get1
back. - Objects are not wrapped. That is, if you do
heap.enqueue(foo)
, then you should doheap.dequeue()
to getfoo
back, iffoo
is an object.
CoderPad has no official support for priority queues.
CodeSignal's official documentation does not claim support for priority queues. However, there is documentation suggesting that heap-js is available.
There are a few other options.
- Insert into an array and sort it afterwards. See simple-heap.ts.
- Use binary search to insert into an array and do not sort. See bs-search-heap.ts.
- Roll your own heap during the interview. You beast.
- Give up.