|
| 1 | +/*Write an algorithm such that if an element |
| 2 | + in an M*N matrix is 0,it's entire row and column |
| 3 | + are set to 0*/ |
| 4 | + |
| 5 | +const zeroMatrix = nestedArr => { |
| 6 | + const rowsAndColsWithZeros = checkForZeroIndex(nestedArr); |
| 7 | + const rowLength = nestedArr[0].length; |
| 8 | + const columnLength = nestedArr.length; |
| 9 | + for (const row in rowsAndColsWithZeros['rows']) { |
| 10 | + for (let col = 0; col < rowLength; col++) { |
| 11 | + nestedArr[row][col] = 0; |
| 12 | + } |
| 13 | + } |
| 14 | + for (const col in rowsAndColsWithZeros['columns']) { |
| 15 | + for (let row = 0; row < columnLength; row++) { |
| 16 | + nestedArr[row][col] = 0; |
| 17 | + } |
| 18 | + } |
| 19 | + return nestedArr; |
| 20 | +}; |
| 21 | + |
| 22 | +// **** Helper function **** |
| 23 | +function checkForZeroIndex(nestArr) { |
| 24 | + let zeroIndices = { rows: {}, columns: {} }; |
| 25 | + for (let row = 0; row < nestArr.length; row++) { |
| 26 | + for (let col = 0; col < nestArr[row].length; col++) { |
| 27 | + if (nestArr[row][col] === 0) { |
| 28 | + zeroIndices['rows'][row] = true; |
| 29 | + zeroIndices['columns'][col] = true; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + return zeroIndices; |
| 34 | +} |
| 35 | + |
| 36 | +// **** TESTS ****: |
| 37 | +let actual = zeroMatrix([[]]); |
| 38 | +let expected = [[]]; |
| 39 | +isEqual(actual, expected); |
| 40 | + |
| 41 | +actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 5], [2, 2, 2]]); |
| 42 | +expected = [[3, 0, 6], [0, 0, 0], [4, 0, 5], [2, 0, 2]]; |
| 43 | +isEqual(actual, expected); |
| 44 | + |
| 45 | +actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 0], [2, 0, 2]]); |
| 46 | +expected = [[3, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]; |
| 47 | +isEqual(actual, expected); |
| 48 | + |
| 49 | +actual = zeroMatrix([[3, 5, 6], [0, 0, 2], [4, 4, 0], [2, 0, 2]]); |
| 50 | +expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]; |
| 51 | +isEqual(actual, expected); |
| 52 | + |
| 53 | +function isEqual(actual, expected) { |
| 54 | + console.log(JSON.stringify(actual) === JSON.stringify(expected)); |
| 55 | +} |
| 56 | + |
| 57 | +// ****HELPER FUNCTION TESTS ****: |
| 58 | +actual = checkForZeroIndex([[1, 2, 0]]); |
| 59 | +expected = { rows: { 0: true }, columns: { 2: true } }; |
| 60 | +testCheckForZeroIndex(actual, expected); |
| 61 | + |
| 62 | +actual = checkForZeroIndex([[0]]); |
| 63 | +expected = { rows: { 0: true }, columns: { 0: true } }; |
| 64 | +testCheckForZeroIndex(actual, expected); |
| 65 | + |
| 66 | +actual = checkForZeroIndex([[1, 2, 3], [4, 0, 5], [6, 0, 8]]); |
| 67 | +expected = { rows: { 1: true, 2: true }, columns: { 1: true } }; |
| 68 | +testCheckForZeroIndex(actual, expected); |
| 69 | + |
| 70 | +function testCheckForZeroIndex(actual, expected) { |
| 71 | + let rows = JSON.stringify(actual.rows); |
| 72 | + let cols = JSON.stringify(actual.columns); |
| 73 | + let expRows = JSON.stringify(expected.rows); |
| 74 | + let expCols = JSON.stringify(expected.columns); |
| 75 | + console.log(rows === expRows && cols === expCols); |
| 76 | +} |
0 commit comments