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

Ports - Ngoc #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
55 changes: 52 additions & 3 deletions lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,57 @@
# 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: ?
#Approach 1:
# Time complexity: O(n^3) 3 nested loops
Copy link

Choose a reason for hiding this comment

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

Yep

# Space complexity: O(n^2) n is the number of rows and columns in the matrix being cloned
Copy link

Choose a reason for hiding this comment

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

This is true if the number of rows and columns are the same (i.e. the input matrix is a "square" matrix). However, if you allow the number of rows & columns to differ, you probably need to specify O(nm) and define n as the number of rows and m as the number of columns.


def matrix_convert_to_zero_n_3(matrix)
clone = matrix.map(&:dup)
rows = matrix.length
columns = matrix[0].length
rows.times do |i|
columns.times do |j|
if matrix[i][j] == 0
columns.times do |jj|
clone[i][jj] = 0
end
rows.times do |ii|
clone[ii][j] = 0
end
end
end
end
rows.times do |i|
columns.times do |j|
matrix[i][j] = clone[i][j]
end
end
end

#Approach 2:
# Time complexity: O(n^2) 2 nested loops
Copy link

Choose a reason for hiding this comment

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

Same comment as above (O(nm) instead of O(n^2)), but well done! This second implementation is a significant improvement over the O(n^3) implementation above.

# Space complexity: O(n) n is the number of affected rows and columns
def matrix_convert_to_zero(matrix)
raise NotImplementedError
affected_rows = []
affected_columns = []
rows = matrix.length
columns = matrix[0].length
rows.times do |i|
columns.times do |j|
if matrix[i][j] == 0
affected_rows << i
affected_columns << j
end
end
end
affected_rows.each do |i|
columns.times do |j|
matrix[i][j] = 0
end
end
affected_columns.each do |j|
rows.times do |i|
matrix[i][j] = 0
end
end
end
11 changes: 6 additions & 5 deletions specs/matrix_convert_to_zero_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/matrix_convert_to_zero'
require "minitest/autorun"
require "minitest/reporters"
require_relative "../lib/matrix_convert_to_zero"

# helper method for creating and initializing a matrix with all 1s
def initialize_matrix(rows, columns)
# create the matrix using the rows and columns
matrix = Array.new(rows){Array.new(columns)}
matrix = Array.new(rows) { Array.new(columns) }

# initialize the matrix
rows.times do |row|
Expand Down Expand Up @@ -47,8 +47,9 @@ def verify_matrix(matrix, rows_array, columns_array)
columns_array = [3, 4]

# method call
p matrix
matrix_convert_to_zero(matrix)

p matrix
# validation
verify_matrix(matrix, rows_array, columns_array)
end
Expand Down