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

0% completed

Vote For New Content
Saving the index of each list instead of their references

mariana.lopez.jaimez

Aug 25, 2024

In the Python solution, when we push a tuple of the form (number, number_index, list), we are actually storing a reference to the list into the heap. Alternatively, we could save the index of each list. For example:

for i, a_list in enumerate(lists): heappush(min_heap, (a_list[0], 0, i)) count = 0 while min_heap: number, number_idx, list_idx = heappop(min_heap) count += 1 if count == k: return number curr_list = lists[list_idx] if len(curr_list) > number_idx + 1: heappush(min_heap, (curr_list[number_idx + 1], number_idx + 1, list_idx))

1

0

Comments
Comments

On this page