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
69 changes: 66 additions & 3 deletions lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,71 @@
# 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(r * c), where r equals the number of arrays (rows) inside the array, and
# c equals the number of elements (columns) inside of a nested array
# Space complexity: O(1), or constant. We're not creating any additional data structures that hinges on user input
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great explanations on time and space complexity.

def matrix_convert_to_zero(matrix)
raise NotImplementedError
# Check for edge cases
return if (matrix == nil || matrix == [])

# Determine length of rows and columns
rows = matrix.length
columns = matrix[0].length

row_zero_zeroes = false
# Does Row 0 have zeros?
columns.times do |col|
if matrix[0][col] == 0
row_zero_zeroes = true
end
end

column_zero_zeroes = false
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'd recommend having all of your variables at the top of your method, that way if you ever need to add or change a variable you do not need to hunt through the method to find it.

# Does Column 0 have zeros?
rows.times do |row|
if matrix[row][0] == 0
column_zero_zeroes = true
end
end

# Check (Rows 1 - n) and (Cols 1 - n)
# Track found zeros through Row 0 and Col 0
# (by changing corresponding element into 0)

(rows - 1).times do |row|
(columns - 1).times do |col|
if matrix[row + 1][col + 1] == 0
matrix[0][col + 1] = 0
matrix[row + 1][0] = 0
end
end
end

# Look at an element inside matrix
# Check corresponding element inside row 0 or col 0
# Are either 0? If so, change targeted element to 0

(rows - 1).times do |row|
(columns - 1).times do |col|
if matrix[0][col + 1] == 0 || matrix[row + 1][0] == 0
matrix[row + 1][col + 1] = 0
end
end
end

# IF Row 0 has zeros: Populate with 0
if row_zero_zeroes
columns.times do |col|
matrix[0][col] = 0
end
end

# IF Col 0 has zeros: Populate with 0
if column_zero_zeroes
rows.times do |row|
matrix[row][0] = 0
end
end

return matrix
end