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), where n is the number of rows and m is the number of columns in the matrix
# Space complexity: O(n), where n is the number of elements in the matrix.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actually I'd say this is O(n) where n is the number of rows or columns in the matrix. You're not building an additional 2D array.

def matrix_convert_to_zero(matrix)
raise NotImplementedError
cols_with_zeros = []
rows_with_zeros = []

matrix.each_with_index do |row, i|
row.each_with_index do |col_num, j|
if col_num == 0
cols_with_zeros.push(j)
rows_with_zeros.push(i)
end
end
end

cols_with_zeros.each do |col|
matrix.length.times do |x|
matrix[x][col] = 0
end
end

rows_with_zeros.each do |row|
matrix.first.length.times do |i|
matrix[row][i] = 0
end
end

return matrix
end