|
| 1 | +/** |
| 2 | + Do not return anything, modify matrix in-place instead. |
| 3 | + */ |
| 4 | + |
| 5 | +// TC: O(m * n) |
| 6 | +// ✅ SC: O(1) |
| 7 | +function setZeroes(matrix: number[][]): void { |
| 8 | + let isFirstRowZero: boolean = false; |
| 9 | + let isFirstColZero: boolean = false; |
| 10 | + |
| 11 | + // check if the first row has any zeros |
| 12 | + for (let col = 0; col < matrix[0].length; col++) { |
| 13 | + if (matrix[0][col] === 0) { |
| 14 | + isFirstRowZero = true; |
| 15 | + break; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + // check if the first column has any zeros |
| 20 | + for (let row = 0; row < matrix.length; row++) { |
| 21 | + if (matrix[row][0] === 0) { |
| 22 | + isFirstColZero = true; |
| 23 | + break; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + // Use the first row and column to mark rows and columns that need to be zeroed |
| 28 | + for (let row = 1; row < matrix.length; row++) { |
| 29 | + for (let col = 1; col < matrix[0].length; col++) { |
| 30 | + if (matrix[row][col] === 0) { |
| 31 | + matrix[row][0] = 0; |
| 32 | + matrix[0][col] = 0; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // Set matrix cells to zero based on markers in the first row and column |
| 38 | + for (let row = 1; row < matrix.length; row++) { |
| 39 | + for (let col = 1; col < matrix[0].length; col++) { |
| 40 | + if (matrix[row][0] === 0 || matrix[0][col] === 0) { |
| 41 | + matrix[row][col] = 0; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Zero out the first row if needed |
| 47 | + if (isFirstRowZero) { |
| 48 | + for (let col = 0; col < matrix[0].length; col++) { |
| 49 | + matrix[0][col] = 0; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Zero out the first column if needed |
| 54 | + if (isFirstColZero) { |
| 55 | + for (let row = 0; row < matrix.length; row++) { |
| 56 | + matrix[row][0] = 0; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +// TC: O(m * n) |
| 63 | +// SC: O(m + n) |
| 64 | +/* |
| 65 | +function setZeroes(matrix: number[][]): void { |
| 66 | +
|
| 67 | + const rows = new Set<number>(); |
| 68 | + const cols = new Set<number>(); |
| 69 | +
|
| 70 | + // Identify all rows and columns that contain at least one zero |
| 71 | + for (let row = 0; row < matrix.length; row++) { |
| 72 | + for (let col = 0; col < matrix[0].length; col++) { |
| 73 | + if (matrix[row][col] === 0) { |
| 74 | + rows.add(row); |
| 75 | + cols.add(col); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | + // Set all elements in the identified rows to zero |
| 81 | + for (const row of rows) { |
| 82 | + for (let col = 0; col < matrix[0].length; col++) { |
| 83 | + matrix[row][col] = 0; |
| 84 | + } |
| 85 | + } |
| 86 | +
|
| 87 | + // Set all elements in the identified columns to zero |
| 88 | + for (const col of cols) { |
| 89 | + for (let row = 0; row < matrix.length; row++) { |
| 90 | + matrix[row][col] = 0; |
| 91 | + } |
| 92 | + } |
| 93 | +}; |
| 94 | +*/ |
| 95 | + |
0 commit comments