From eda929376c382b120625d544b3b38ed47b57e307 Mon Sep 17 00:00:00 2001 From: Vibhanshu Singh <135852806+singhvibhanshu@users.noreply.github.com> Date: Mon, 21 Oct 2024 22:17:53 +0530 Subject: [PATCH] Create matrix_zero_problem.py --- data_structures/arrays/matrix_zero_problem.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 data_structures/arrays/matrix_zero_problem.py diff --git a/data_structures/arrays/matrix_zero_problem.py b/data_structures/arrays/matrix_zero_problem.py new file mode 100644 index 000000000000..b67fa5c20c28 --- /dev/null +++ b/data_structures/arrays/matrix_zero_problem.py @@ -0,0 +1,54 @@ +def setZeroes(matrix): + rows = len(matrix) + cols = len(matrix[0]) + + # To keep track if we need to zero out the first row and first column + first_row_zero = False + first_col_zero = False + + # Check if the first column should be zero + for i in range(rows): + if matrix[i][0] == 0: + first_col_zero = True + + # Check if the first row should be zero + for j in range(cols): + if matrix[0][j] == 0: + first_row_zero = True + + # Mark zero rows and columns by using the first row and first column + for i in range(1, rows): + for j in range(1, cols): + if matrix[i][j] == 0: + matrix[i][0] = 0 + matrix[0][j] = 0 + + # Set matrix cells to zero based on marks + for i in range(1, rows): + for j in range(1, cols): + if matrix[i][0] == 0 or matrix[0][j] == 0: + matrix[i][j] = 0 + + # Zero out the first row if needed + if first_row_zero: + for j in range(cols): + matrix[0][j] = 0 + + # Zero out the first column if needed + if first_col_zero: + for i in range(rows): + matrix[i][0] = 0 + +# Example input +matrix = [ + [1, 1, 1], + [1, 0, 1], + [1, 1, 1] +] + +# Call the function +setZeroes(matrix) + +# Print the result +for row in matrix: + print(row)