Created
November 11, 2018 23:14
-
-
Save AlexeiDarmin/97e5f66a743e520ad759c4bfc2e4821f to your computer and use it in GitHub Desktop.
If an element in an NxM matrix is zero, then every element in that row and column should be zero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If an element in an NxM matrix is zero, then every element in that row and column should be zero | |
function zeroOutMatrix(m: number[][]): number[][] { | |
const zeroedRows = new Map() | |
const zeroedCols = new Map() | |
for (let r = 0; r < m.length; ++r) { | |
for (let c = 0; c < m[r].length; c++){ | |
if (m[r][c] === 0) { | |
zeroedCols.set(c, true) | |
zeroedRows.set(r, true) | |
} | |
} | |
} | |
for (let r = 0; r < m.length; ++r) { | |
if (zeroedRows.get(r) === true) { | |
m[r] = m[r].map(v => 0) | |
} | |
} | |
for (let c = 0; c < m[0].length; ++c) { | |
if (zeroedRows.get(c) === true) { | |
for (let r = 0; r < m.length; ++r) { | |
m[r][c] = 0 | |
} | |
} | |
} | |
return m | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment