Find the K numbers from small to large in a sort matrix

Source: Internet
Author: User

A topic description

In a sort matrix, find the first k integer from small to large.

The sort matrix is defined as: Each row is incremented, and each column is incremented.

Two

Because each row in the sort matrix is incremented, and each column is incremented. From small to large number of k, is actually the number of K small. Ideas are as follows:

Assuming that the sort matrix has row rows and Col columns, because each row is incremented, we simply select the minimum number of rows per row (altogether row) and choose the smallest number from the row number to repeat the process K, the minimum value of the K-th is the smallest number in the whole matrix.

The code is as follows:

Class Solution{public:/** * @param matrix:a matrix of integers * @param k:an integer * @return: The kth Smallest number in the matrix */int kthsmallest (vector<vector<int>> &matrix, int k) {Si        ze_t row = Matrix.size ();        size_t col = matrix[0].size ();        if (row = = 0 | | col = = 0) return-1; The int minrows[row];//is used to store the number of columns corresponding to the minimum value of each row memset (minrows,0,sizeof (minrows));//Initialize the minrows with an initial value of 0 if (k > row * C        OL)//error handling return-1;        int RS;        int tmp; for (int cnt = 1; CNT <= K; cnt++) {int min_val = int_max;//Each comparison needs to be initialized, note the position of the variable definition for (int Row_index = 0; Row_index < row;                    row_index++) {if (Minrows[row_index] < col)//Note This judging condition must be added to prevent cross-border { if (Matrix[row_index][minrows[row_index]] < Min_val) {min_val = Matrix [Row_index]      [Minrows[row_index]];                  TMP = Row_index; }}} minrows[tmp]++;//update the position of the minimum value in the corresponding row, note that the position here is after the row wheel loop to find the minimum if (cnt = = k        ) rs = Min_val;    } return RS; }};

The algorithm is simple, the time complexity of the algorithm is O (k*row), in addition, there are several points to note:

1. The minimum value we select each time refers to the minimum value in the row number, so the mi_val variable needs to be defined at the beginning of each loop, where I define INT_MAX, so all the rows need to be traversed one by one times.

2. In each row wheel cycle, do not forget to determine whether the corresponding value of the number of columns of the index has reached the maximum value.

3. Note that the Minrows array is updated only after the end of the row round cycle.

============================================================================================================

In addition, the subject is considered from the point of view of the column, that is, each time to find the minimum value of a column, and of course, can be considered from the angle of the line, each time to find the minimum number of rows, repeat K, the minimum value of the K-th is the final result. The two approaches are similar, with the second method, the time complexity becomes O (k*col).

When the number of columns is greater than the number of rows, the first method is better, and when the number of rows is greater than the number of columns with the second method is better, of course, the premise is that the difference between the number of rows and columns is relatively large, otherwise the two methods of performance is similar; of course, can also be combined with these two cases,

Find the K numbers from small to large in a sort 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.