From 10fcd612e17973071f1579e99714131ecb4a8824 Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 1 May 2019 18:30:53 -0700 Subject: [PATCH 1/2] . --- lib/matrix_convert_to_zero.rb | 41 ++++++++++++++++++++++++++-- specs/matrix_convert_to_zero_spec.rb | 9 +++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/matrix_convert_to_zero.rb b/lib/matrix_convert_to_zero.rb index 4fb139c..2b005c2 100644 --- a/lib/matrix_convert_to_zero.rb +++ b/lib/matrix_convert_to_zero.rb @@ -3,8 +3,43 @@ # 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: the time complexity is O(n*m) where n is the number of rows in the matrix and m is the number of columns in the matrix because I have two nested loops O(2*n*m), which dropping the constant reduces to O(n*m) +# Space complexity: space complexity is O(n) where n is the larger number of rows or columns. This is because a hash is created where the rows are keys and columns are values def matrix_convert_to_zero(matrix) - raise NotImplementedError + num = 0 + pos = Hash.new + rows = matrix.size + columns = matrix[0].size + + rows.times do |row| + columns.times do |col| + if matrix[row][col] == 0 + if pos.include? row + num += 1 + pos["row" + num.to_s] = col + else + pos[row] = col + end + end + end + end + rows.times do |row| + columns.times do |col| + if pos.include? row + matrix[row][col] = 0 + end + if pos.values.include? col + matrix[row][col] = 0 + end + end + end + return matrix end + +# matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1], [1, 1, 0]] + +# p matrix_convert_to_zero(matrix) +matrix = [[1, 1, 1, 0, 0], [1, 1, 1, 1, 1], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1]] +# matrix = [[0, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]] + +p matrix_convert_to_zero(matrix) diff --git a/specs/matrix_convert_to_zero_spec.rb b/specs/matrix_convert_to_zero_spec.rb index a21b0a6..b563e9a 100644 --- a/specs/matrix_convert_to_zero_spec.rb +++ b/specs/matrix_convert_to_zero_spec.rb @@ -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| @@ -129,6 +129,7 @@ def verify_matrix(matrix, rows_array, columns_array) matrix[0][3] = 0 # row 0, column 1 matrix[0][4] = 0 # row 1, column 1 matrix[2][1] = 0 # row 2, column 1 + p matrix rows_array = [0, 2] columns_array = [1, 3, 4] From 578527018536351a595a169c1627eb226484479a Mon Sep 17 00:00:00 2001 From: Cyndi Lopez Date: Wed, 1 May 2019 18:31:35 -0700 Subject: [PATCH 2/2] remove unnecessary comments --- lib/matrix_convert_to_zero.rb | 8 -------- 1 file changed, 8 deletions(-) diff --git a/lib/matrix_convert_to_zero.rb b/lib/matrix_convert_to_zero.rb index 2b005c2..4c9a90a 100644 --- a/lib/matrix_convert_to_zero.rb +++ b/lib/matrix_convert_to_zero.rb @@ -35,11 +35,3 @@ def matrix_convert_to_zero(matrix) end return matrix end - -# matrix = [[1, 1, 1], [1, 0, 1], [1, 1, 1], [1, 1, 0]] - -# p matrix_convert_to_zero(matrix) -matrix = [[1, 1, 1, 0, 0], [1, 1, 1, 1, 1], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1]] -# matrix = [[0, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]] - -p matrix_convert_to_zero(matrix)