Skip to content

Commit cf85236

Browse files
Merge pull request ignacio-chiazzo#30 from ignacio-chiazzo/prob
Added set mattrix zeroes
2 parents e60887e + 4736d89 commit cf85236

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

LeetcodeProblems/Set_Matrix_Zeroes.js

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
Set Matrix Zeroes
3+
https://leetcode.com/problems/set-matrix-zeroes/
4+
5+
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
6+
7+
Example 1:
8+
9+
Input:
10+
[
11+
[1,1,1],
12+
[1,0,1],
13+
[1,1,1]
14+
]
15+
Output:
16+
[
17+
[1,0,1],
18+
[0,0,0],
19+
[1,0,1]
20+
]
21+
Example 2:
22+
23+
Input:
24+
[
25+
[0,1,2,0],
26+
[3,4,5,2],
27+
[1,3,1,5]
28+
]
29+
Output:
30+
[
31+
[0,0,0,0],
32+
[0,4,5,0],
33+
[0,3,1,0]
34+
]
35+
Follow up:
36+
37+
A straight forward solution using O(mn) space is probably a bad idea.
38+
A simple improvement uses O(m + n) space, but still not the best solution.
39+
Could you devise a constant space solution?
40+
*/
41+
42+
/**
43+
* @param {number[][]} matrix
44+
* @return {void} Do not return anything, modify matrix in-place instead.
45+
*/
46+
var setZeroes = function(matrix) {
47+
if(matrix.length === 0)
48+
return;
49+
50+
var pivotRow = -1;
51+
var pivotCol = -1;
52+
var iterRow = 0;
53+
var iterCol = 0;
54+
var found = false;
55+
56+
// Find a pivot
57+
while(!found && iterRow < matrix.length) {
58+
iterCol = 0;
59+
while(!found && iterCol < matrix[0].length) {
60+
if(matrix[iterRow][iterCol] === 0) {
61+
found = true
62+
pivotRow = iterRow;
63+
pivotCol = iterCol;
64+
}
65+
iterCol++;
66+
}
67+
iterRow++;
68+
}
69+
70+
if (!found)
71+
return;
72+
73+
// Update the Column value
74+
for(var i = 0; i < matrix.length; i++) {
75+
if(i == pivotRow)
76+
continue
77+
for(var j = 0; j < matrix[0].length; j++) {
78+
if(j == pivotCol)
79+
continue;
80+
if(matrix[i][j] === 0) {
81+
matrix[i][pivotCol] = 0;
82+
matrix[pivotRow][j] = 0;
83+
}
84+
}
85+
}
86+
87+
for(var i = 0; i < matrix.length; i++)
88+
if(matrix[i][pivotCol] === 0 && i !== pivotRow)
89+
fillRow(matrix, i);
90+
91+
for(var i = 0; i < matrix[0].length; i++)
92+
if(matrix[pivotRow][i] === 0 && i !== pivotCol)
93+
fillCol(matrix, i);
94+
95+
fillCol(matrix, pivotCol);
96+
fillRow(matrix, pivotRow);
97+
};
98+
99+
var fillRow = function(matrix, row) {
100+
for(var i = 0; i < matrix[0].length; i++)
101+
matrix[row][i] = 0;
102+
}
103+
104+
var fillCol = function(matrix, col) {
105+
for(var i = 0; i < matrix.length; i++)
106+
matrix[i][col] = 0;
107+
}
108+
109+
var main = function() {
110+
console.log(setZeroes([[1,1,1],[1,0,1],[1,1,1]]));
111+
}
112+
113+
module.exports.main = main;

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Solutions of algorithm problems using Javascript
66

77
| Name | Level | Link |
88
| - | - | - |
9+
| [Set Matrix Zeroes](/LeetcodeProblems/Set_Matrix_Zeroes.js) | Hard | https://leetcode.com/problems/set-matrix-zeroes/ |
910
| [Edit Distance ](/LeetcodeProblems/Edit_Distance.js) | Hard | https://leetcode.com/problems/edit-distance/ |
1011
| [Remove Invalid Parentheses ](/LeetcodeProblems/Remove_Invalid_Parentheses.js) | Hard | https://leetcode.com/problems/remove-invalid-parentheses/ |
1112
| [Longest Consecutive Sequence ](/LeetcodeProblems/Longest_Consecutive_Sequence.js) | Hard | https://leetcode.com/problems/longest-consecutive-sequence/ |

0 commit comments

Comments
 (0)