Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hayden - Edges (C10) - Matrix Covert To Zero #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
19 changes: 18 additions & 1 deletion lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,22 @@
# 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.
def matrix_convert_to_0(matrix)
raise NotImplementedError
matrix.each do |row|
index = 0
row.each do |element|
if element == 0
row.length.times do |i|
row[i] = 0
end
matrix.length.times do |i|
matrix[i][index] = 0
end
end

index += 1
end
end
end

# the time complexity of this solution is O(n * m) where n is the number of elements in each row and m is the number of elements in each column
# the space complexity is O(1) because the only space required in memory by this solution is a handful of iteration variables. All transformations are performed in place.