Skip to content

Files

Latest commit

f3fcce8 · Nov 12, 2022

History

History

13_k_way_merge

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Nov 4, 2022
Nov 12, 2022
Jul 26, 2022
Jul 26, 2022
Jul 26, 2022
Jul 26, 2022
Nov 12, 2022
Jul 26, 2022

Introduction

This pattern helps us solve problems that involve a list of sorted arrays.

Whenever we are given ‘K’ sorted arrays, we can use a Heap to efficiently perform a sorted traversal of all the elements of all arrays. We can push the smallest (first) element of each sorted array in a Min Heap to get the overall minimum.

While inserting elements to the Min Heap we keep track of which array the element came from. We can, then, remove the top element from the heap to get the smallest element and push the next element from the same array, to which this smallest element belonged, to the heap. We can repeat this process to make a sorted traversal of all elements.