Algorithm Data Structure interview questions -- Application of labeled arrays in matrix Feature Recognition

Source: Internet
Author: User

Question: There is a matrix (10*10). The element value can only be 0 or 1. Now, write a function to check whether a row is 1, and each column is 0 (except that this element of the row is 1 ). The program needs to scan the matrix once to obtain the result.

Source: Internet, said to be from Microsoft interview questions.
Answer:
Without considering the requirements of the last sentence of the question, the most direct method is as follows:
bool F(int [10][10]input){   for(int i=0;i<10;i++)   {      int j;      for(j=0;j<10;j++)         if(input[i][j]==0) break;      if(j==10)      {         for(int m=0;m<10;m++)         {            for(int n=0;n<10;n++)               if(input[n][m]==1&&n!=i) break;            if(n==10)               return true;         }      }   }   return false;}
It can be seen that this algorithm will re-scan the entire matrix after each row of element is found to be 1. Theoretically, the time complexity of mxn matrix operations is O (Mn + mxmxn) = O (mxmxn ). But considering the actual situation, it is certainly not up to this limit, but the efficiency is already very low.
In this case, you can consider setting another bool colallzero [N] array, which is initially true. If column J of the row is 1 when row I is scanned in a second-level loop, colallzero [J] is marked as false if column J of the row is 1.
In this way, only two loops are required, and a scan is performed at a time. The algorithm is as follows.
Bool F (INT [10] [10] input) {bool colallzero [10]; for (Int J = 0; j <10; j ++) colallzero [J] = true; For (INT I = 0; I <10; I ++) {Int J; For (j = 0; j <10; j ++) if (input [I] [J] = 0) break; If (j <10) // It is 0 when [I] [J] is scanned, {for (INT m = 0; m <j; m ++) colallzero [m] = false; // mark 0 to the J-1 column does not meet all 0} else // J is 10, that is, the row I is scanned, the row meets the requirements {for (INT m = 0; m <10; m ++) {If (! Colallzero [m]) continue; For (INT n = 0; n <I-1; I ++) {If (input [N] [m] = 1) {colallzero [m] = false; break;} If (n! = I-1) continue; // [N] [m] is not 0 and does not meet the requirements. // otherwise, m column 0 to I-1 rows meet all 0 requirements if (I = 9) // The row number meeting the requirements is 9th rows, that is, the last row returns true; // The recognition ends, for [I] [m] For (INT n = I + 1; n <10; I ++) // The row meeting the requirements is on top of the last line {If (input [N] [m] = 1) {colallzero [m] = false; break ;}} if (n = 10) return true; // The m column of row I meets the requirements} return false ;}
Note that although the algorithm is still a triple loop, the analytical complexity of the algorithm will find that it is O (Mn), not O (MMN) or O (Mnn ). Because columns that do not meet the requirements but whose values are 1 in the second loop are marked by colallzero, these columns marked as false will not be accessed in the third loop. Therefore, the time complexity is O (Mn ).
 
-

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.