Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,32 @@
# 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: ?
# Space complexity: ?
# Time complexity: O(n * m) or O(n^2) where n == m
# Space complexity: O(n), n > m where n & m are the dimension of the matrix
def matrix_convert_to_zero(matrix)
raise NotImplementedError
# raise NotImplementedError
row_set = Set.new
column_set = Set.new

for i in (0...matrix.length)
for j in (0...matrix[i].length)
if matrix[i][j] == 0
row_set.add(i)
column_set.add(j)
end
end
end

row_set.each_with_index do |k|
for l in (0...matrix[k].length)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions:

1). Why use .each_with_index for the outer loop if you don't end up using the index variable.
2). Why use a for-in loop here if you do an each with index for the outer loop?

matrix[k][l] = 0
end
end

for n in (0...matrix.length)
column_set.each_with_index do |m|
matrix[n][m] = 0
end
end
end