-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathch1-q8.js
137 lines (120 loc) · 3.24 KB
/
ch1-q8.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
/**
* Do a first pass through the matrix to find which cells have 0's. When a 0 is
* found then mark that row and column as needing to be zeroed out. On the second
* pass zero out any cells that need to be zeroed out based on the row or column
* indicators.
*
* N = matrix Y dimension
* M = matrix X dimension
* Time: O(N * M)
* Additional space: O(N + M)
*
* @param {array} matrix Matrix to be zeroed in-place
* @return {array} Matrix that has been zeroed, same object as input
*/
export function zeroMatrix(matrix) {
if (!Array.isArray(matrix) || !Array.isArray(matrix[0])) {
throw new Error('invalid matrix');
}
if (matrix.length === 0) {
return matrix;
}
let rows = new Array(matrix.length),
cols = new Array(matrix[0].length);
rows.fill(false);
cols.fill(false);
for (let y = 0; y < rows.length; ++y) {
for (let x = 0; x < cols.length; ++x) {
if (matrix[y][x] === 0) {
rows[y] = true;
cols[x] = true;
}
}
}
for (let y = 0; y < rows.length; ++y) {
for (let x = 0; x < cols.length; ++x) {
if (rows[y] || cols[x]) {
matrix[y][x] = 0;
}
}
}
return matrix;
}
/**
* Instead of using two extra arrays as storage for rows and columns that will be
* nullified, use the first row and first column.
*
* N = matrix Y dimension
* M = matrix X dimension
* Time: O(N * M)
* Additional space: O(1)
*
* @param {array} matrix Matrix to be zeroed in-place
* @return {array} Matrix that has been zeroed, same object as input
*/
export function zeroMatrixConstantSpace(matrix) {
if (!Array.isArray(matrix) || !Array.isArray(matrix[0])) {
throw new Error('invalid matrix');
}
if (matrix.length === 0) {
return matrix;
}
// check if first row has a zero
let firstRowHasZero = false;
for (let col = 0; col < matrix[0].length; col++) {
if (matrix[0][col] === 0) {
firstRowHasZero = true;
break;
}
}
// check if first column has a zero
let firstColHasZero;
for (let row = 0; row < matrix.length; row++) {
if (matrix[row][0] === 0) {
firstColHasZero = true;
break;
}
}
// check for zeros in the rest of the rows/cols and update
// first row/column storage accordingly
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[0].length; col++) {
if (matrix[row][col] === 0) {
matrix[row][0] = 0;
matrix[0][col] = 0;
}
}
}
// nullify rows based on the values in the first column
for (let row = 1; row < matrix.length; row++) {
if (matrix[row][0] === 0) {
nullifyRow(matrix, row);
}
}
// nullify columns based on the values in the first row
for (let col = 1; col < matrix[0].length; col++) {
if (matrix[0][col] === 0) {
nullifyCol(matrix, col);
}
}
// nullify first row if necessary
if (firstRowHasZero) {
nullifyRow(matrix, 0);
}
// nullify first col if necessary
if (firstColHasZero) {
nullifyCol(matrix, 0);
}
return matrix;
}
function nullifyRow(matrix, row) {
for (let col = 0; col < matrix[row].length; col++) {
matrix[row][col] = 0;
}
}
function nullifyCol(matrix, col) {
for (let row = 0; row < matrix.length; row++) {
matrix[row][col] = 0;
}
}