|
| 1 | +// Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and columns are set to 0. |
| 2 | + |
| 3 | +const assert = require("assert"); |
| 4 | + |
| 5 | +/** |
| 6 | + * Overwrite rows and columns who have at least 1 zero in them with zeros. |
| 7 | + * @param {Array.<number[]>} matrix input 2d array to mutate |
| 8 | + * @return {null} |
| 9 | + * |
| 10 | + * We start by initializing rows and cols arrays with false values. We iterate through the matrix, and when we find a 0, |
| 11 | + * we flag its row and column by updating the corresponding indices in rows and cols for future use. |
| 12 | + * When we take a second pass at the matrix, we update all rows that have been flagged to 0, and all columns that have |
| 13 | + * been flagged to 0, which is O(M + N). |
| 14 | + * |
| 15 | + * Runtime: O(M * N) |
| 16 | + * Space: O(M + N) |
| 17 | + * |
| 18 | + */ |
| 19 | +const zeroMatrix = (arr) => { |
| 20 | + if (arr.length === 0 || arr[0].length === 0) return; |
| 21 | + const rows = new Array(arr.length).fill(false); |
| 22 | + const cols = new Array(arr[0].length).fill(false); |
| 23 | + |
| 24 | + for (let i = 0; i < arr.length; i++) { |
| 25 | + for (let j = 0; j < arr[0].length; j++) { |
| 26 | + if (arr[i][j] !== 0) continue; |
| 27 | + rows[i] = true; |
| 28 | + cols[j] = true; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + for (let i = 0; i < arr.length; i++) { |
| 33 | + if (!rows[i]) continue; |
| 34 | + for (let j = 0; j < arr[0].length; j++) { |
| 35 | + arr[i][j] = 0; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + for (let j = 0; j < arr[0].length; j++) { |
| 40 | + if (!cols[j]) continue; |
| 41 | + for (let i = 0; i < arr.length; i++) { |
| 42 | + arr[i][j] = 0; |
| 43 | + } |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +describe(module.filename, () => { |
| 48 | + it("should not affect a matrix of size 0", () => { |
| 49 | + const matrix = []; |
| 50 | + |
| 51 | + zeroMatrix(matrix); |
| 52 | + assert.deepEqual(matrix, []); |
| 53 | + }); |
| 54 | + it("should not affect a matrix without 0s", () => { |
| 55 | + const matrix = [ |
| 56 | + [1, 2, 3], |
| 57 | + [4, 5, 6], |
| 58 | + [7, 8, 9], |
| 59 | + ]; |
| 60 | + |
| 61 | + zeroMatrix(matrix); |
| 62 | + assert.deepEqual(matrix, [ |
| 63 | + [1, 2, 3], |
| 64 | + [4, 5, 6], |
| 65 | + [7, 8, 9], |
| 66 | + ]); |
| 67 | + }); |
| 68 | + it("should update all rows and columns with at least one 0 to all 0s", () => { |
| 69 | + const matrix = [ |
| 70 | + [0, 2, 3, 1], |
| 71 | + [4, 5, 6, 1], |
| 72 | + [7, 8, 9, 1], |
| 73 | + ]; |
| 74 | + |
| 75 | + zeroMatrix(matrix); |
| 76 | + assert.deepEqual(matrix, [ |
| 77 | + [0, 0, 0, 0], |
| 78 | + [0, 5, 6, 1], |
| 79 | + [0, 8, 9, 1], |
| 80 | + ]); |
| 81 | + }); |
| 82 | + it("should update all rows and columns with at least one 0 to all 0s when multiple 0s are present", () => { |
| 83 | + const matrix = [ |
| 84 | + [0, 2, 3], |
| 85 | + [4, 5, 0], |
| 86 | + [7, 8, 9], |
| 87 | + ]; |
| 88 | + |
| 89 | + zeroMatrix(matrix); |
| 90 | + assert.deepEqual(matrix, [ |
| 91 | + [0, 0, 0], |
| 92 | + [0, 0, 0], |
| 93 | + [0, 8, 0], |
| 94 | + ]); |
| 95 | + }); |
| 96 | +}); |
0 commit comments