* Additional Space
* Array - Tabulation
* Time O(ROWS * COLS) | Space (ROWS + COLS)
* https://leetcode.com/problems/set-matrix-zeroes/
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function (matrix) {
const [rows, cols] = [matrix.length, matrix[0].length];
const [_row, _col] = initTabu(rows, cols);
fillPlacements(
matrix,
_row,
_col,
);
setZero(matrix, _row, _col);
};
const initTabu = (rows, cols) => [
new Array(rows).fill(1) ,
new Array(cols).fill(1) ,
];
const fillPlacements = (matrix, _row, _col) => {
const [rows, cols] = [matrix.length, matrix[0].length];
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const isZero = matrix[row][col] === 0;
if (!isZero) continue;
_row[row] = 0;
_col[col] = 0;
}
}
};
const setZero = (matrix, _row, _col) => {
const [rows, cols] = [matrix.length, matrix[0].length];
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const canSet = _row[row] === 0 || _col[col] === 0;
if (!canSet) continue;
matrix[row][col] = 0;
}
}
};
* Constant Space
* Time O(ROWS * COLS) | Space (1)
* https://leetcode.com/problems/set-matrix-zeroes/
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = (matrix) => {
const _isColZero = isColZero(matrix);
setEdgesToZero(matrix);
setCellsToZero(matrix, _isColZero);
};
var isColZero = (matrix) =>
matrix.some((row) => row[0] === 0);
var setEdgesToZero = (matrix) => {
const [rows, cols] = [matrix.length, matrix[0].length];
for (let row = 0; row < rows; row++) {
for (let col = 1; col < cols; col++) {
const canSet = matrix[row][col] === 0;
if (!canSet) continue;
matrix[row][0] = 0;
matrix[0][col] = 0;
}
}
};
var setCellsToZero = (matrix, isColZero) => {
const [rows, cols] = [matrix.length, matrix[0].length];
for (let row = rows - 1; 0 <= row; row--) {
for (let col = cols - 1; 1 <= col; col--) {
if (!isZero(matrix, row, col)) continue;
matrix[row][col] = 0;
}
if (isColZero) matrix[row][0] = 0;
}
};
var isZero = (matrix, row, col) => {
const [rowLeftEdge, colTopEdge] = [matrix[row][0], matrix[0][col]];
return rowLeftEdge === 0 || colTopEdge === 0;
};
* Constant Space
* Time O(ROWS * COLS) | Space (1)
* https://leetcode.com/problems/set-matrix-zeroes/
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = (matrix) => {
const isColZero = setEdgesToZero(matrix);
setCellsToZero(matrix);
const isZero = matrix[0][0] === 0;
if (isZero) setFirstRowZero(matrix);
if (isColZero) setFirstColZero(matrix);
};
var setCellsToZero = (matrix) => {
const [rows, cols] = [matrix.length, matrix[0].length];
for (let row = 1; row < rows; row++) {
for (let col = 1; col < cols; col++) {
const isZero = matrix[row][0] === 0 || matrix[0][col] == 0;
if (!isZero) continue;
matrix[row][col] = 0;
}
}
};
var setEdgesToZero = (matrix, isColZero = false) => {
const [rows, cols] = [matrix.length, matrix[0].length];
for (let row = 0; row < rows; row++) {
if (matrix[row][0] === 0) isColZero = true;
for (let col = 1; col < cols; col++) {
const canSet = matrix[row][col] === 0;
if (!canSet) continue;
matrix[0][col] = 0;
matrix[row][0] = 0;
}
}
return isColZero;
};
var setFirstRowZero = (matrix, cols = matrix[0].length) => {
for (let col = 0; col < cols; col++) {
matrix[0][col] = 0;
}
};
var setFirstColZero = (matrix, rows = matrix.length) => {
for (let row = 0; row < rows; row++) {
matrix[row][0] = 0;
}
};