Merge k Sorted Arrays 🛑
Vložit
- čas přidán 17. 05. 2022
- This is the video under the series of DATA STRUCTURE & ALGORITHM in a BINARY HEAP Playlist. Now we are going to code Heap in Data Structure or Implement. in this video we Solve a Programming Question from the geeksforgeeks Heap Tag Question on the Merge k Sorted Arrays a Medium Tag Question From heap Data structure. Let's learn Heaps series by Hello world by Prince.
Join My Telegram channel for more Updates: telegram.me/helloworldbyprince
complete DSA preparation: github.com/Prince-1501/Comple...
----------------------------------------------------------------------------------------
Merge k Sorted Arrays: practice.geeksforgeeks.org/pr...
----------------------------------------------------------------------------------------
*Follow me *
LinkedIn► / iamprince
Facebook► / helloworldofficials
Instagram► / helloworldbyprince
Twitter► / prince_king_
Telegram► telegram.me/helloworldbyprince
----------------------------------------------------------------------------------------
►Our Playlists on:-
► Tree: • Tree Data Structure & ...
► Stack & Queue: • Stack & Queue Data Str...
► Hashing: • Hashing Data Structure...
► Graph: • Graph Data Structure &...
► Matrix: • Matrix (Multidimension...
► Heap: • Heap Data Structure & ...
► STL: • Standard Template Libr...
► Leetcode: • LeetCode Solutions And...
►Competitive Programming: • Full course in Competi...
►C++ Full Course : • C++ full Course in HINDI
►Algorithms: • L-01 || Prefix Sum Arr...
►Data Structure: • Data Structures with C...
------------------------------------------------------------------------
🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
✨ Tags ✨
merge k sorted arrays
merge k sorted arrays leetcode
merge k sorted arrays using a priority queue
merge k sorted arrays gfg practice
merge k sorted arrays gfg
merge k sorted arrays python
merge k sorted arrays using priority queue java
merge k sorted arrays coding ninjas
merge k sorted arrays using heap
merge k sorted arrays interviewbit
merge k sorted arrays divide and conquer
merge k sorted arrays interviewbit solution
merge k sorted arrays techie delight
merge k sorted arrays priorityqueue java
Heap sort playlist Hello world
Heap sort practice problems
heap practice problems gfg
leetcode heap questions
leetcode heap
heap hello world
Types of the heap in Data structure & algorithms
playlist heap Hindi
question asked in Google
off-campus placement
Practice heap data structure
heap in a data structure in Hindi
heap Full playlist for Beginners
algorithms
heap
data structure
gate computer science preparation
programming languages
Comment "#Princebhai" if you read this 😉😉
#Heap #merge_k_sorted_arrays #programming
good video! worth watching. Conceptual clarity.
awesome explanation, able to code in java after your explanation ,no need to see your code, Thanks man
Your channel is really amazing, and your explanation too. Thanks for helping me to improve logic
Happy to hear that!
love the you explaind with ease and with detail
bhaiya bhot acha video hai bhot hardwork kar rahe ho app Thank you so much bhaiya.
One of the best vedio
bhaiya puraa samaj aagya after watching this video 2-3 times but still samaj aagya thank you❤❤
That's a clean explanation
this video deserves more likes and views . great explanation .
Appreciate the efforts man. Best explaination available for this question on youtube in my opinion 💪
Bhaiya keep it up very nice explanation
its a great explanation brother.sach m maza a gaya
made this qs so clear and easy Thanks!!
You're welcome! Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
this video deserves more likes and views . very well explained
Understood very well.
nice
Bhaiya ur style of teaching is so amazing , u make things really simple
Thank you so much 😀
Best Explaination prince Sir.....
Thank You Sir............
Hare Krishna..! Great Video
Many many thanks
thank you sir for very nice explanation
Thanks and welcome
Great Explanation.
Glad it was helpful!
Bhai ye maine khud kiya hai pairs ka use karke.
Sirf aapki vajah se. Thanks. 🙏
typedef pair pi;
class Solution
{
public:
//Function to merge k sorted arrays.
vector mergeKArrays(vector arr, int K)
{
vector ans;
priority_queue< pi, vector, greater >pq;
//Step 1
// Pushing all first element of all SubArrays in Min Heap.
int size = arr.size();
for(int i=0; i
Best❤
Teacher nhi ye koi jadu jo itna axa samjhaye aay hay
Hahaha Thanks for the support and love
Outstanding explanation , it's far more better then paid courses , Now , I myself see your videos instead of the courses I bought....I have a small doubt instead of using struct mycomp i'm using similar
static bool mycomp function but it's not working can you explain why
great explanation sir.
Keep watching
The sweat is the proof of your hard work
Very well explained sir.
Thanks yaar
Great video, this deserves more likes!
Oh thank you
what will be the time complexity of this soltuion please tell sir
Nice concept
thank you so much sir ♥
Keep learning buddy and keep growing
Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
Thanks!!
Keep learning yaar
This video really helps. One request please, plesae, please make a video on stl comparator function, how it works.
you can refer to this : czcams.com/video/3pvZhwp0U9w/video.html
Thanks bro sure
bhiya shi me smjh aaya gfg k course se nhi ayaa tha jabki
bhaiya hume aaj first aisa solution hai linkedlist bst tree stacks queue sara padh liya aisa comperator use karke jo isme bna rhe hai ye samjh hi nhi aaya bilkul demotivate ho gya tha then i get ur videos
really great, working tooo hard
Thank you! Cheers! Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
Hi it was really a great approach that you have used. I just wanted to know if this is some common approach which is used to solve this question or this is the approach that you have come up with?
WHAT WILL BE THE TIME COMPLEXITY FOR THIS SIR COULD YOU PLZ EXPLAIN??
Prince Bhai... ❤️
your welcome Saurabh
Thank youu for these, if there has to merge linked lists instead of arrays, can we use the same approach?
Yesss u can
Thank you Bhaiya
your welcome buddy
best video
We can also use greater right instead of bool comparator to make it min heap?
Lekin greater < int > me data type to int hai na
Humlog custom data type ke liye wo kaam nahi karega we have to make it out Own
@@HelloWorldbyprince ok
can you tell time complexity and space complexity bcs for interview perspective it is very important to know tc and sc so from now onwards include tc and sc in your video
O(K2*Log(K))
data d means we are calling constructor ?? and if we are calling constructor using d then how can we put three values in one heap
no one was explaining how we are inserting the next element but suddenly it kicks and understood , example of finding first k fastest horses
yess Tanveer good catch
can we do pair instead of data
thanks
Welcome Please, share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
🔥🔥🔥🔥
Thanks
What is the TIme complexity and space complexity
❤❤❤❤❤❤
❤️
Good explanation
Keep watching
Understood
Nice 👍
What is the need if comparator? and which values does d1 and d2 denote
yeah i have also same doute
so the TC is KlogK ?
Nice
Thanks
#princebhai
done by myself...thank you.masum.
multisets;
for(int i=0;i
Thanks yaar
I have only one doubt, how do you make comparison structure. please bhaiya make a separate video on this.
sure bro i will make one video
god lvl explanation
Keep watching
Superb
Thanks 🤗
Helpful video❤ but what is time and space complexity?
thanks
maja aa gaya
Thanks bro
Understanding++
Thanks a lot
yar main point hai comperator kaise use kar rhe h wo samjh hi nhi aaya wahi phash ja rha hun mai baki sara to use karte hi aa rh hun but comperator new h use samjhao aap achge se
Correct me if I'm its space complexity is O(N) - We are filling our custom data structure and putting it in pq , Maximum elements in data and in pq will always be N
class Solution
{
public:
//Function to merge k sorted arrays
void heapify(vector&arr,int n,int i)
{
int largest = i;
int left = 2*i + 1;
int right = 2 *i + 2;
if(left < n and arr[left] > arr[largest])
{
largest = left;
}
if(right < n and arr[right] > arr[largest])
{
largest = right;
}
if(largest != i)
{
swap(arr[i],arr[largest]);
heapify(arr,n,largest);
}
}
void build_heap(vector&arr,int n)
{
// for internal nodes
// build heap from an array
for(int i = (n/2-1);i>=0;i--)
{
heapify(arr,n,i);
}
}
void heap_sort(vector&arr,int n)
{
build_heap(arr,n);
for(int i = n-1;i>=0;i--)
{
swap(arr[0],arr[i]);
heapify(arr,i,0);
}
}
vector mergeKArrays(vector arr, int K)
{
//code here
vectorans;
for(int i = 0;i
Yesss u r right 😂😂
What is time complexity of efficient approach
O(K2*Log(K))
Hello Prince
why do we take next item from the same array? what is intuition behind that?
ek baar aap please dry run karke dekho na code ko jo maine bataya hai i promise aapko samjh me aa jayega agar fir v nahi aayega to i here to help u
straight forward intuition: - array sorted hone ki Wajah se, jis array se minimum aayega, ussi array se next time bhi minimum aane ke jyada chances honge.
Jai shree krishna
Bhaiya mycompt struct internally work kaise kr raha h please bta do
sure bro i will definitely
Logic is crytal clear and explnation is crystal clear .
i write my own code for this much simpler then your code bhaiya no use of comperrator no need to define a seperate class
typedef typedef pair pii;
vector mergeKArrays(vector arr, int k)
{
vector ans;
priority_queue pq;
for(int i =0 ; i0)
{
ans.push_back(pq.top().first);
int row = pq.top().second.first;
int col = pq.top().second.second;
if(col+1
This code doesn't make any sense bro here the TC is O(N * k * log (N * k)). which can be achieve even after using 2 for Loops and storing all elements in Min heap!!
This question should be in hard level
hmm
bhai is mai operator overload kiya wo samj nahi aaya
✅ TC -> O(NKlogK)
✅ SC -> O(NK), for filling our user defined data type,
where , K denotes number of input arrays
N denotes number of maximum elements in an array
🙂🙂
please enlighten me if i am wrong
in worst case the time and space complexity would be O(k^2*logk) and O(k^2)
I don't understand the concept of compare
sorry but give some time dhere dhere sammjh me aa jayega
time complexity?
O(K2*Log(K))
Sir ap = curr.apos & vp = curr.vpos kese find hogaa
ek baar aap dry run karke dekho aapko pakka samjh me aa jayega bro
@@HelloWorldbyprince ok Saamja me aagaye thank you
Sab sahi tha bas comparator smj nhi aya ...
aa jayega bro thora time do
instead of using concept of data we can use pair
but this code is more readable
mycomp smjh nhi aaya
time ke sath aa jayega, first time its tough
What is the time complexity of this min heap approach ?
O(n*logk)
HOT STUFF 🥵🥵
Hahah
My solution::::
class Pair{
int key;
int i,j;
Pair(int key,int i,int j){
this.key=key;
this.i=i;
this.j=j;
}
}
class Solution
{
//Function to merge k sorted arrays.
public static ArrayList mergeKArrays(int[][] arr,int K)
{
// Write your code here.
ArrayList l=new ArrayList();
PriorityQueue q=new PriorityQueue(new Comparator(){
public int compare(Pair p1,Pair p2){
return p1.key-p2.key;
}
});
for(int i=0;i
last example dry run done : 4 5 6 9 10
nice yaar aap bhut punctual ho good job