Source: https://leetcode.com/problems/set-matrix-zeroes/
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to zeroes. You must do it in place.
/*** @param {number[][]} matrix* @return {void} Do not return anything, modify matrix in-place instead.*/// Approach: Keep track of rows and cols that have 0s in separate sets// Loop through the matrix and whenever we encounter a 0, we had the row index to a row set and col index to a col set// Using the row and col sets representing where we should zero out, we can loop again through the matrix and always check the row/col sets to determine if we should zero out the existing element// O(mn) time, O(m + n) spacevar setZeroes = function(matrix) {// Initialize sets for row and column indices that should be zeroed outconst rowZeroes = new Set();const colZeroes = new Set();// Loop through the matrix and whenever we encounter a zero, we add the row index to the row set and// col index to the col setfor (let row = 0; row < matrix.length; row++) {for (let col = 0; col < matrix[0].length; col++) {const currentValue = matrix[row][col];if (currentValue === 0) {rowZeroes.add(row);colZeroes.add(col);}}}// Loop through the matrix again and check the row and col sets to determine if we should zero out the current elementfor (let row = 0; row < matrix.length; row++) {for (let col = 0; col < matrix[0].length; col++) {if (rowZeroes.has(row) || colZeroes.has(col)) {matrix[row][col] = 0;}}}};