forked from AdaGold/matrix-convert-to-zero
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathmatrix_convert_to_zero.rb
More file actions
47 lines (40 loc) · 1.09 KB
/
matrix_convert_to_zero.rb
File metadata and controls
47 lines (40 loc) · 1.09 KB
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
# Updates the input matrix based on the following rules:
# Assumption/ Given: All numbers in the matrix are 0s or 1s
# If any number is found to be 0, the method updates all the numbers in the
# corresponding row as well as the corresponding column to be 0.
# Time complexity: O(m * n), where m is the number of rows in the input matrix and n is the number of columns
# Space complexity: O(m + n), where m is the number of rows in the input matrix and n is the number of columns
def matrix_convert_to_zero(matrix)
x_zeroes = {}
y_zeroes = {}
rows = matrix.length
columns = matrix[0].length
i = 0
until i == rows
array = matrix[i]
j = 0
until j == columns
if array[j] == 0
x_zeroes[i] = true
y_zeroes[j] = true
end
j += 1
end
i += 1
end
rows.times do |row|
if x_zeroes[row] == true
columns.times do |column|
matrix[row][column] = 0
end
end
end
columns.times do |column|
if y_zeroes[column] == true
rows.times do |row|
matrix[row][column] = 0
end
end
end
return matrix
end