Notação de grande Oh (NM) ou (n^2)

disseram-me que o código abaixo é = O(MN) no entanto, eu venho com O(n^2). Qual é a resposta correcta e porquê?

O meu processo de pensamento: aninhado para loops plus if statements -- > (O (n^2)+O (1)) + (O (N^2)+O (1)) = o(n^2)

Obrigado

public static void zeroOut(int[][] matrix) { 
    int[] row = new int[matrix.length];
    int[] column = new int[matrix[0].length];
// Store the row and column index with value 0 
 for (int i = 0; i < matrix.length; i++)
   {
    for (int j = 0; j < matrix[0].length;j++) { 
       if (matrix[i][j] == 0) 
          {
            row[i] = 1;
            column[j] = 1; 
          }
   } 
  }
// Set arr[i][j] to 0 if either row i or column j has a 0 
for (int i = 0; i < matrix.length; i++)
 {
   for (int j = 0; j < matrix[0].length; j++)
      { 
        if ((row[i] == 1 || column[j] == 1)){
              matrix[i][j] = 0; 
           }
      } 
  }
}
Author: KingJahfy, 2013-01-29

1 answers

A que se referem M E N? Minha suposição é que se refere a "linhas" e "colunas" respectivamente. Se for assim, então a equação é O (MN) porque você faz um loop através do número M de N vezes.

O (n^2) estará correcto se as linhas e colunas forem iguais.

 9
Author: ggbranch, 2013-01-29 03:45:30