forked from ignacio-chiazzo/Algorithms-Leetcode-Javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet_Matrix_Zeroes.js
113 lines (96 loc) · 2.11 KB
/
Set_Matrix_Zeroes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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;