Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion src/chapter1/ch1-q8.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

/**
/** 1st Solution
* Do a first pass through the matrix to find which cells have 0's. When a 0 is
* found then mark that row and column as needing to be zeroed out. On the second
* pass zero out any cells that need to be zeroed out based on the row or column
Expand Down Expand Up @@ -47,3 +47,57 @@ export function zeroMatrix(matrix) {

return matrix;
}


/** 2nd Solution
* Do a first pass through the matrix to find which cells have 0's. When a 0 is
* found then mark it in the first column and row. Then check the first row to
* see which columns to zero out. Repeat the process for the first column to
* see which rows to zero out.
*
* N = matrix Y dimension
* M = matrix X dimension
* Time: O(N * M)
* Additional space: O(1)
*
* @param {array} matrix Matrix to be zeroed in-place
* @return {array} Matrix that has been zeroed, same object as input
*/

const zero = (matrix) => {

let rows = matrix.length,
cols = matrix[0].length;

//First pass through to indicate which cols and row to zero out
for (let i = 0; i < rows; ++i) {
for (let j = 0; j < cols; ++j) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0
matrix[0][j] = 0;
}
}
}

//zero out colums by looking at first row
for (let j = 0; j < cols; ++j) {
if (matrix[0][j] == 0) {
for (let i = 0; i < rows; ++i) {
matrix[i][j] = 0;
}
}
}

//zero out rows by looking at first col
for (let i = 0; i < cols; ++i) {
if (matrix[i][0] == 0) {
for (let j = 0; j < rows; ++j) {
matrix[i][j] = 0;
}
}
}

return matrix;
}