Skip to content

Solution #23 - Daniel/Edited - 18.03.2025 #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Merge K Sorted Arrays Solution

## Overview

The goal of this solution is to merge k sorted arrays into a single sorted array.

## Possible Approaches

Comment on lines +1 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that you have given three approaches. Likewise kindly add the code for all 3 approaches, rather than just one.

### 1. Sorting

- Create result array
- Append all elements of all k lists to the array
- Sort the array

#### Time Complexity

O((N*K)*log(N\*K)), K is number of arrays and N is average number of elements in the array

#### Space Complexity

O(N\*K)

Traversing all k lists, sorting the final list

### 2. Merge Sort

- Create a helper function that performs merge sort on two arrays using two pointers

- Call the function in pairs to effectively half the number of arrays every iteration.

- Return the output array

#### Time Complexity

O((N*K)*log(K))

Log(k) levels of recursion calls, and at each level, K arrays are traversed for merging.

#### Space Complexity

O((N*K)*log(K))

Log(k) levels and O(N\*K) space is required to store the answer every call.

### 3. Min Heap

- Most straightforward approach so far and the one implemented.
- Iterate through all of the k arrays and append to a min heap implemented by a priority queue.
- At each pass, append a single element from each array.
- Pop the min element and insert the next element from which the element is extracted. If empty, array is merged.
- Traverse the priority queue to store in a vector.
- Return the vector

#### Time Complexity

O((N*K)*log(k))

Min-heap of size k used. For insertion and removal of elements, the time complexity is N \* Klog(K).

#### Space Complexity

O(N\*K)

Min-heap of size K used, for arrays averaging N elements.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <bits/stdc++.h>
vector<int> mergeKSortedArrays(vector<vector<int>>&kArrays, int k)
{
priority_queue<int, vector<int>, greater<int>> q;
vector<int> result;
for(int i=0; i<kArrays.size(); i++){
for(int j=0; j<kArrays[i].size(); j++){
q.push(kArrays[i][j]);
}
}
while(!q.empty()){
result.push_back(q.top());
q.pop();
}
return result;
}