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

Sockets - Nidhi #11

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
33 changes: 30 additions & 3 deletions lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +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.

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n^2)
# Space complexity: If a matrix is rows*columns large, than space complexity will be O(rows) or O(columns), depending on which is larger.
def matrix_convert_to_zero(matrix)
raise NotImplementedError
rows = matrix.length
columns = matrix[0].length

sum_rows = []
matrix.each do |row|
sum_rows.push(row.sum)
end

sum_columns = []
columns.times do |i|
sum = 0
rows.times do |j|
sum = sum + matrix[j][i]
end
sum_columns.push(sum)
end

max = rows + columns

rows.times do |i|
columns.times do |j|
if sum_rows[i] + sum_columns[j] < max

Choose a reason for hiding this comment

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

I have to say, this is really clever. Nicely done. It does require you to have two arrays, one for rows and the other for columns.

matrix[i][j] = 0
end
end
end

return matrix
end