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

tests passing matrix_convert_to_zero #16

Open
wants to merge 4 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
32 changes: 31 additions & 1 deletion lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,35 @@
# 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.length).times do |i| #number of rows times do runs 3 times
(matrix[i].length).times do |j| #number of columns this runs 4 times
if matrix[i][j] == 0 #if something in this row/column is 0
row = i
column = j #select the column
(matrix[row].length).times do |j| #change the whole row to 0
matrix[row][j] = 0
end
(matrix.length).times do |i|
matrix[i][column] = 0
end
end
end
end
return matrix
end

# Space Complexity:
# Regardless of the side of the matrix, we need to create two variables
#row and column to hold the position of the element that is 0.
# The row and column variables will only contain 1 integer each,
#which stays the same, thefore it is Constant O(1) Space Complexity.
#
# Time Complexity:
# There are 4 loops in this solution. The first loop will perform n times
# (where n is the number of rows that the matrix contains). The second loop
#will perform m times (where m is the number of columns the matrix contains).
# If 0 is found in a row/column combination, in the worst case the third and fourth
# loops will run row.length * column.length times, or n x n, or n**2.
# Overall, the time complexity is O(n*m) + n **2
#
#