leetcode:378. Kth smallest Element in a Sorted Matrix

Source: Internet
Author: User

Topic:

Given a n x n Matrix where each of the rows and columns is sorted in ascending order, find the kth smal Lest element in the matrix.

Note that it was the kth smallest element in the sorted order and not the kth distinct element.

Example:

Matrix = [   [1,  5,  9],   [Ten, one, +],   [15]],k = 8,return 13.
Give a nxn matrix, each row, each column is incremented, we want to find the smallest K value. Note: Repeat numbers are allowed. (I wanted to use set.) Honestly used list)

Method:
No high-level algorithm, win in the idea of simple ... Think about writing a workable version first.
The key to this approach is:
    1. Set up a list separately, each time from the inside to filter the minimum value out of the stack. (Simply call it "stack") for the first column, but not the last line of the element out of the stack, it will be the right and the following elements into the stack.
    2. The first column, but the element of the last row is out of the stack, the element to the right is stacked.
    3. For a non-first column element, if it is not the last column, the element to the right of it is in the stack. For elements that are not the first column but the last column is out of the stack, nothing is done (that is, the last element in each row).
    4. The 00 position must be the first minimum value, so for k>1, initialize the 01,10 position into the list to start filtering the minimum value in the list. Recall the first step.

Note: matrices have only 1 elements, and k=1 two special cases.

Code:

#include <iostream> #include <algorithm> #include <list>using namespace std;typedef struct node{int ro    W    int lie; int data;}    Node;node popmin (list<node> & Compare) {auto Iter=compare.cbegin ();    Node min;    Min= (*iter);        while (Iter!=compare.cend ()) {if (min.data> (*iter). Data) {min= (*iter);    } iter++;    }//Current minimum value out of stack iter=compare.cbegin ();            while (Iter!=compare.cend ()) {if (*iter). Data==min.data) {compare.erase (ITER);        Break    } iter++; }//Returns the minimum value, information includes: Minimum data, minimum value in row, column return min;}    int main (int argc, const char * argv[]) {vector<vector<int>> matrix={{1,2},{1,3}};    The row and column values of the Matrix Auto Matrixrow=matrix.size ();    Auto Matrixlie=matrix[0].size ();    list<node>compare;    int k=1;    int count=1;    Node min;        int ans=0;//returns the K small element if (matrixrow>1) {//Initialize compare doubly linked list node Tempnode; Tempnode.data=matrix[0][1];        tempnode.row=0;        tempnode.lie=1;        Compare.push_back (Tempnode);        TEMPNODE.DATA=MATRIX[1][0];        Tempnode.row=1;        tempnode.lie=0;        Compare.push_back (Tempnode);                if (1<k) {while (count<k) {min=popmin (compare); count++;//each find a minimum value, count+1//First column if (min.lie==0) {if (Min.row<matrixrow                        -1) {//Not last line//bottom into stack tempnode.data=matrix[min.row+1][min.lie];                        cout<<tempnode.data<<endl;                        tempnode.row=min.row+1;                        Tempnode.lie=min.lie;                        Compare.push_back (Tempnode);                        Right into the stack tempnode.data=matrix[min.row][min.lie+1];                        cout<<tempnode.data<<endl;                       Tempnode.row=min.row; tempnode.lie=min.lie+1;                    Compare.push_back (Tempnode); }else if (min.row==matrixrow-1) {//last line//right into stack tempnode.data=matrix[min.row][m                        IN.LIE+1];                        cout<<tempnode.data<<endl;                        Tempnode.row=min.row;                        tempnode.lie=min.lie+1;                    Compare.push_back (Tempnode);                        }}else{//non-first column if (min.lie<matrixlie-1) {//Not last column//right-hand stack                        TEMPNODE.DATA=MATRIX[MIN.ROW][MIN.LIE+1];                        cout<<tempnode.data<<endl;                        Tempnode.row=min.row;                        tempnode.lie=min.lie+1;                    Compare.push_back (Tempnode);  }else if (min.lie==matrixlie-1) {//a column///No continue that can be placed on the stack;                  }}} Ans=min.data;        }else if (k==1) {ans=matrix[0][0];    }}else{ans=matrix[0][0];    } cout<<ans<<endl; return 0;}

  

leetcode:378. Kth smallest Element in a Sorted Matrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.