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

Trang Frego - Matrix Convert to Zero #14

Open
wants to merge 1 commit 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
47 changes: 46 additions & 1 deletion lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,51 @@
# Assumption/ Given: All numbers in the matrix are 0s or 1s
# 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: O(n x m)
# Space Complexity: O(n) + O(m)
# n being the row length and m being the column length

def matrix_convert_to_0(matrix)
raise NotImplementedError
rows = matrix.size
columns = matrix[0].size
columns_with_zeros = []
rows_with_zeros = []

i = 0

while i < rows
j = 0
while j < columns
if matrix[i][j] == 0
if !columns_with_zeros.include?(j)
columns_with_zeros << j
end
if !rows_with_zeros.include?(i)
rows_with_zeros << i
end
end
j += 1
end
i += 1
end

columns_with_zeros.each do |column|
i = 0
while i < rows
matrix[i][column] = 0
i += 1
end
end

rows_with_zeros.each do |row|
j = 0
while j < columns
matrix[row][j] = 0
j += 1
end
end

return matrix

end