 What's up guys, I'm Michael and welcome to my YouTube channel today We're gonna go over another leak code challenge of Kate's smallest element in a sordid matrix So you're given an end-by-end matrix for each of the row column are sorted in ascending order and find the case smallest element in the matrix so It's exactly the same thing as case smalls element in a array In the only difference is that it's now a matrix. Okay, so We could use a min heap and then keep store all the values of the matrix into that min heap And then iterate from the beginning to get the kth Kth smallest element, but that would take O of log of n squared. Yeah over the log of n squared, which would take Which is takes a lot of space So what we're gonna do is we're gonna use a max heap and we're gonna go through every element in the matrix And then we will compare the each element if it's less than the value in the max heap we'll remove the value of the max the top value of the max heap and then at the at the end the The first value of our max heap is gonna be the smallest element in the matrix the kth smallest element in the matrix and If you don't understand what I'm talking about you could watch this some other videos that I made about How to do this, but yeah Okay, so we're gonna go go through every value in the matrix and Then what we're gonna do is we're going to create a max heap So prior to queues are already max heaps based on the In C++ if you want to make it a min heap you have to change this to a greater at a comparator In your priority queue, but normally it's already max heap. So Okay, if our max heap size Is less than k We are going to add just the value of that We are going to add the current value we're at in a matrix. Otherwise, we are going to check check if the Current value we're gonna add is less than The top value of our max heap and if that's the case, we're gonna remove the top value of our max heap And then we're gonna push it at it also So by the end we're gonna have all the kth smallest elements in our In our pretty much in our array Okay, and then at the end of this we're gonna return turn max heap top submit And it got accepted. So yeah, that's how you do this problem. Uh, it's pretty basic Break com subscribe. I'll check you guys later