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
42 lines (36 loc) · 875 Bytes
/
matrix_convert_to_zero.rb
File metadata and controls
42 lines (36 loc) · 875 Bytes
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
# 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 x n)
# Space complexity: O(1)
require "awesome_print"
def matrix_convert_to_zero(matrix)
column_zero = 1
rows = matrix.length
cols = matrix[0].length
for i in 0...rows
if matrix[i][0] == 0
column_zero = 0
end
for j in 1...cols
if matrix[i][j] == 0
matrix[i][0] = matrix[0][j] = 0
end
end
end
i = rows - 1
while i >= 0
j = cols - 1
while j >= 1
if matrix[i][0] == 0 || matrix[0][j] == 0
matrix[i][j] = 0
end
j -= 1
end
if column_zero == 0
matrix[i][0] = 0
end
i -= 1
end
end