Skip to content

Added set mattrix zeroes #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions LeetcodeProblems/Set_Matrix_Zeroes.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/ |
Expand Down