Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
FYI: you can just put the lists directly into the min heap

Ike Nwankwo

Nov 7, 2023

The provided python solution uses a tuple containing, the first element, index of said element, and the list itself. However python heaps will by default determine priority using the first element of the list, meaning you don't need to keep track of all of that.

1

0

Comments
Comments
I
Ike Nwankwo2 years ago

See my solution: from heapq import *

class Solution: def findKthSmallest(self, lists, k): number = 0

TODO: Write your code here

min_heap = [] for i, lst in enumerate(lists): heappush(min_heap,lst)

while k > 0: if min_heap: lst = heappop(min_heap) num = lst[0] number...

On this page