以下のコードは = O(MN) であると言われましたが、O(N^2) を思いつきました。正しい答えはどれですか?またその理由は?
私の思考プロセス: ネストされた for ループと if ステートメント --> (O(N^2)+O(1)) + (O(N^2)+O(1)) = O(N^2)
ありがとうございました
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;
}
}
}
}