Kth Largest Element in a Stream – Python3 solution

In python, it is possible to transform a list into a priority queue where the top element is the element with the highest priority (and the highest value (in the case of a list of integers)).

Source: programiz

heapq — Heap queue algorithm in python helps transform a list into a priority queue.

heapq.heapify(x)
// Transform list x into a heap, in-place, in linear time.

Example:

Example

Input: [ 3, [4, 5, 6], [5, 7, 2] ]

Output: [4, 5, 5]

Explanation: (k=3)

Solution in Python3

Don’t miss these tips!

We don’t spam! Read our privacy policy for more info.

Open chat
Powered by