From c3e6b6b3e9ab903de87954e0edd79f585f1d40fc Mon Sep 17 00:00:00 2001 From: Jabez Sam Sunder Date: Mon, 17 Mar 2025 22:24:00 +0530 Subject: [PATCH] Solution #73 - Jabez/Edited - 17/3/2025 --- .../Set Matrix Zeroes - Explanation.md | 29 +++++++++++++ .../Set Matrix Zeroes - Solution.java | 42 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 Arrays101/#73-Set Matrix Zeroes-Medium/Set Matrix Zeroes - Explanation.md create mode 100644 Arrays101/#73-Set Matrix Zeroes-Medium/Set Matrix Zeroes - Solution.java diff --git a/Arrays101/#73-Set Matrix Zeroes-Medium/Set Matrix Zeroes - Explanation.md b/Arrays101/#73-Set Matrix Zeroes-Medium/Set Matrix Zeroes - Explanation.md new file mode 100644 index 0000000..91112d3 --- /dev/null +++ b/Arrays101/#73-Set Matrix Zeroes-Medium/Set Matrix Zeroes - Explanation.md @@ -0,0 +1,29 @@ +What This Code Does: +* Find zeros in the matrix + +* If a cell is 0, mark its row and column to be zero later. +* Use the first row & column as markers + +* To track if first row and column have zeros we use firstrow and firstcol boolean variables + +* Instead of creating extra memory, the first row and column store which rows/columns should be zero. +* Set the marked cells to zero + +* Go through the matrix and make elements 0 based on the markers. +* Handle the first row and column separately + +If the first row/column originally had a 0, set the entire first row/column to 0. + +Imagine this matrix: + + +1 2 3 +4 0 6 +7 8 9 + +After running the code, it becomes: + +1 0 3 +0 0 0 +7 0 9 +The 0 at (1,1) caused its entire row and column to be zero. \ No newline at end of file diff --git a/Arrays101/#73-Set Matrix Zeroes-Medium/Set Matrix Zeroes - Solution.java b/Arrays101/#73-Set Matrix Zeroes-Medium/Set Matrix Zeroes - Solution.java new file mode 100644 index 0000000..e934a4c --- /dev/null +++ b/Arrays101/#73-Set Matrix Zeroes-Medium/Set Matrix Zeroes - Solution.java @@ -0,0 +1,42 @@ +class Solution{ + public void SetMatrixZeroes(int[][] matrix) { + boolean firstrow = false, firstcol = false; // Used to check if the first row and first column contain any zeros + + // Iterate through the matrix to find zeros + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[0].length; j++) { + if (matrix[i][j] == 0) { // If an element is 0, mark its row and column in the first row and first column + if (i == 0) firstrow = true; // Mark that the first row has a zero + if (j == 0) firstcol = true; // Mark that the first column has a zero + + // Store zero markers in the first row and first column + matrix[i][0] = 0; + matrix[0][j] = 0; + } + } + } + + // Use the markers in the first row and column to set matrix elements to zero + for (int i = 1; i < matrix.length; i++) { + for (int j = 1; j < matrix[0].length; j++) { + if (matrix[i][0] == 0 || matrix[0][j] == 0) { + matrix[i][j] = 0; + } + } + } + + // If the first row was marked, set all elements in the first row to zero + if (firstrow) { + for (int j = 0; j < matrix[0].length; j++) { + matrix[0][j] = 0; + } + } + + // If the first column was marked, set all elements in the first column to zero + if (firstcol) { + for (int i = 0; i < matrix.length; i++) { + matrix[i][0] = 0; + } + } + } +}