![sort vector 2d sort vector 2d](https://image.freepik.com/free-vector/vector-set-sport-characters_107173-13257.jpg)
#Sort vector 2d update#
update the hash map after the insertion insert the next smallest number in the min heap go to next smallest number in that row if the extracted element is not the last one in its row removing the extracted minimum elemnt from the min heap getting the column idx of the extracted element in the getting the row of the minimum element extracted maps row_index to same_row's_element_index hash-map to store current idx of every row iterator to point to the extract min_heap element Int kth_smallest_element(int n, int m, int k, vector > &mat)
![sort vector 2d sort vector 2d](http://qleroemporium.weebly.com/uploads/1/3/5/0/135034654/423092909_orig.jpg)
Input space complexity of O(n*m), the size of the given matrix mat and auxiliary space used id O(n). We are maintaining a BST of size O(n) and a hashmap of size O(n) to store the row and columns of the elements in the BST. O(n) where n is number of rows of matrix. So, time complexity will be O(n*log(n)) + O(k*log(n)) → O((n+k)*log(n)). To build a BST by adding first element of each row will take O(n*log(n)), extracting elements k times and inserting elements k time will take O(k*log(n)). O((n+k)*log(n)) where n is number of rows of matrix and k is the element number which need to be extracted. We will use hashmap to map elements in the heap to its row and column number so that we can easily point to the next element to be inserted. So, we repeat this extract and insert operation for k times and hence, the last element extracted in this sequence will be the kth smallest element in the matrix mat. Doing so ensures that every time we extract the minimum element from the matrix. So, we insert the element that belongs to ith row and (j+1)th column of matrix in the BST. Hence, the next smallest element will be the (j+1)th element. But as according to given constraint the columns are also in sorted order. So, we will insert the next smallest element after x from the ith row. Suppose the element extracted is x and it belongs to i row and j column. Each time we extract an element from the BST we insert another element in the BST which will be the next element in the same row as of extracted element. Now, we will start extracting the min-elements from the BST. We will initialise the BST by adding elements of the first column of the matrix mat. Here, we will be maintaining a BST of size n (number of rows in mat). Sort(all_elements.begin(), all_elements.end()) īST based solution.
![sort vector 2d sort vector 2d](https://image.freepik.com/free-vector/game-platform-cartoon-forest-landscape-2d-design_107791-3246.jpg)
storing all elements of 2D matrix in a linear array container to store all the elemnts of the 2D matrix Int kth_smallest_element(int n,int m, int k,vector &mat) k: integer k, denoting kth element to be returned mat: 2D matrix with rows and column sorted in ascending order Space Complexity:įor storing input it will take space of O(n*m) and auxiliary space used is O(n*m). To store array containing all elements of matrix, and as size of matrix will be n*m. O(n*m) where n is number of rows of matrix and m is number of columns of matrix. Hence, the total Time Complexity of the solution will be O(n*m*log(n*m)). Sorting the array takes O(size*log(size)) time (where size = n*m) and accessing the (k-1)th element is a O(1) time operation. O(n*m*log(n*m)) where n is number of rows of matrix and m is number of columns of matrix.
![sort vector 2d sort vector 2d](https://media.giphy.com/media/JyMC8xiMB4EtG/giphy.gif)
We can solve this easily by first sorting the resulting array and hence then the (k-1)th index element will be the kth smallest element (assuming 0-based indexing). Then our task it to find the kth smallest element in the resulting array. We can just push all the elements of the matrix mat into a linear container like an array. The brute-force approach for this problem is pretty much simple. We have provided solutions which contain necessary comments to understand the approach used: Now, the 6th smallest element is none other than 6 itself and hence the answer. There are total 9 elements in the matrix, which when arranged in ascending order are as follows. Return an integer denoting the kth smallest element of the input 2D matrix. The fourth and last parameter of the function is the 2D matrix mat. The third parameter is the integer k, denoting the kth smallest element to be returned by the function. Your algorithm should be efficient enough, so that it suffice q such queries.įirst and second parameter of the function kth_smallest_element, is n and m, depicting the number of rows and columns in the matrix. Your task is to find the kth smallest element in matrix mat. Each row of the matrix contains m integers in ascending order and also each column contains n integers in ascending order. Given a 2D matrix mat of integers with n rows and m columns.