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
32 changes: 29 additions & 3 deletions lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,34 @@
# 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^3) - n * n * m, where n is the length of the largest row/col
# and m is the length of the smallest row/col
# Space complexity: O(1) - constant
def matrix_convert_to_zero(matrix)
raise NotImplementedError
rows = matrix.length
cols = matrix[0].length

rows.times do |row|
cols.times do |col|
matrix[row][col] = "x" if matrix[row][col] == 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of temporary state tracking here! 👍

(This technique becomes super-relevant when you start working with things like graphs and trees.)

end
end

rows.times do |row|
cols.times do |col|
if matrix[row][col] == "x"
cols.times do |i|
matrix[row][i] = 0 if matrix[row][i] != "x"
end

rows.times do |i|
matrix[i][col] = 0 if matrix[i][col] != "x"
end

matrix[row][col] = 0
end
end
end

return matrix
end