<aside> 💡 Iterate and create arrays of rows and columns to be nulled. Iterate again and null them.
</aside>
https://leetcode.com/problems/set-matrix-zeroes/
function setZeroes(matrix) {
const rows = [];
const cols = [];
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[row].length; col++) {
if (matrix[row][col] === 0) {
rows.push(row);
cols.push(col);
}
}
}
for (let row = 0; row < matrix.length; row++) {
if (rows.includes(row)) {
matrix[row].fill(0);
}
for (let col = 0; col < matrix[row].length; col++) {
if (cols.includes(col)) {
matrix[row][col] = 0;
}
}
}
}
<aside> 💡 Set initial boundaries, update after every direction change.
</aside>
https://leetcode.com/problems/spiral-matrix/
function spiralOrder(matrix) {
const visited = [];
let left = 0;
let top = 0;
let right = matrix[0].length - 1;
let bottom = matrix.length - 1;
let direction = "right";
while (left <= right && top <= bottom) {
if (direction === "left") {
// ...
}
if (direction === "right") {
// ...
}
if (direction === "top") {
// ...
}
if (direction === "bottom") {
// ...
}
}
return visited;
}