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
29 lines (27 loc) · 1.02 KB
/
matrix_convert_to_zero.rb
File metadata and controls
29 lines (27 loc) · 1.02 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
# 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(n^2) where n is the width of the matrix. Or O(w*h), where w is the width, and h is the height.
# Space complexity: O(1) constant, regardless of the size of the matrix, a constant number of fixed sixed variables are needed.
def matrix_convert_to_zero(matrix)
rows = matrix.size
columns = matrix[0].size
rows.times do |row|
columns.times do |column|
if matrix[row][column] == 0
matrix[row][column] = 2
end
end
end
rows.times do |row|
columns.times do |column|
if matrix[row][column] == 2
columns.times { |i| matrix[row][i] = 0 unless matrix[row][i] == 2 }
rows.times { |i| matrix[i][column] = 0 unless matrix[i][column] == 2 }
matrix[row][column] = 0
end
end
end
return matrix
end