Merge Sort

Merge k Sorted Lists

January 6, 2024
medium
Divide and Conquer, Merge Sort

Problem # You are given an array of k linked lists, each containing nodes of sorted integers. Merge all the linked lists into one sorted linked list and return its head. Solution Approach # Divide: Recursively divide the list of lists into two halves until each sublist contains only one or zero lists. Conquer: Merge each pair of sublists using a merge function that combines two sorted lists into a single sorted list. ...

Kth Largest Element in K Sorted Arrays

December 19, 2023
medium
Merge Sort, Heap Sort, PriorityQueue

Problem # Given K sorted arrays of integers, find the Kth largest element among all the elements in the arrays. Solution # Here’s the Python code for finding the Kth largest element among all elements in given sorted arrays: import heapq def kth_largest_in_sorted_arrays(arrays, k): # Create a min heap min_heap = [] # Initialize heap with the last element of each array for i, array in enumerate(arrays): if array: heapq. ...