From 4736d8963d837885a9711280ad0471fef1787c84 Mon Sep 17 00:00:00 2001 From: Ignacio Chiazzo Date: Mon, 21 Jan 2019 19:12:17 -0500 Subject: [PATCH] Added set mattrix zeroes --- LeetcodeProblems/Set_Matrix_Zeroes.js | 113 ++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 114 insertions(+) create mode 100644 LeetcodeProblems/Set_Matrix_Zeroes.js diff --git a/LeetcodeProblems/Set_Matrix_Zeroes.js b/LeetcodeProblems/Set_Matrix_Zeroes.js new file mode 100644 index 0000000..a13df28 --- /dev/null +++ b/LeetcodeProblems/Set_Matrix_Zeroes.js @@ -0,0 +1,113 @@ +/* +Set Matrix Zeroes +https://leetcode.com/problems/set-matrix-zeroes/ + +Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. + +Example 1: + +Input: +[ + [1,1,1], + [1,0,1], + [1,1,1] +] +Output: +[ + [1,0,1], + [0,0,0], + [1,0,1] +] +Example 2: + +Input: +[ + [0,1,2,0], + [3,4,5,2], + [1,3,1,5] +] +Output: +[ + [0,0,0,0], + [0,4,5,0], + [0,3,1,0] +] +Follow up: + +A straight forward solution using O(mn) space is probably a bad idea. +A simple improvement uses O(m + n) space, but still not the best solution. +Could you devise a constant space solution? +*/ + +/** + * @param {number[][]} matrix + * @return {void} Do not return anything, modify matrix in-place instead. + */ +var setZeroes = function(matrix) { + if(matrix.length === 0) + return; + + var pivotRow = -1; + var pivotCol = -1; + var iterRow = 0; + var iterCol = 0; + var found = false; + + // Find a pivot + while(!found && iterRow < matrix.length) { + iterCol = 0; + while(!found && iterCol < matrix[0].length) { + if(matrix[iterRow][iterCol] === 0) { + found = true + pivotRow = iterRow; + pivotCol = iterCol; + } + iterCol++; + } + iterRow++; + } + + if (!found) + return; + + // Update the Column value + for(var i = 0; i < matrix.length; i++) { + if(i == pivotRow) + continue + for(var j = 0; j < matrix[0].length; j++) { + if(j == pivotCol) + continue; + if(matrix[i][j] === 0) { + matrix[i][pivotCol] = 0; + matrix[pivotRow][j] = 0; + } + } + } + + for(var i = 0; i < matrix.length; i++) + if(matrix[i][pivotCol] === 0 && i !== pivotRow) + fillRow(matrix, i); + + for(var i = 0; i < matrix[0].length; i++) + if(matrix[pivotRow][i] === 0 && i !== pivotCol) + fillCol(matrix, i); + + fillCol(matrix, pivotCol); + fillRow(matrix, pivotRow); +}; + +var fillRow = function(matrix, row) { + for(var i = 0; i < matrix[0].length; i++) + matrix[row][i] = 0; +} + +var fillCol = function(matrix, col) { + for(var i = 0; i < matrix.length; i++) + matrix[i][col] = 0; +} + +var main = function() { + console.log(setZeroes([[1,1,1],[1,0,1],[1,1,1]])); +} + +module.exports.main = main; diff --git a/README.md b/README.md index 45430f9..f3a6bbc 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ Solutions of algorithm problems using Javascript | Name | Level | Link | | - | - | - | +| [Set Matrix Zeroes](/LeetcodeProblems/Set_Matrix_Zeroes.js) | Hard | https://leetcode.com/problems/set-matrix-zeroes/ | | [Edit Distance ](/LeetcodeProblems/Edit_Distance.js) | Hard | https://leetcode.com/problems/edit-distance/ | | [Remove Invalid Parentheses ](/LeetcodeProblems/Remove_Invalid_Parentheses.js) | Hard | https://leetcode.com/problems/remove-invalid-parentheses/ | | [Longest Consecutive Sequence ](/LeetcodeProblems/Longest_Consecutive_Sequence.js) | Hard | https://leetcode.com/problems/longest-consecutive-sequence/ |