From 542452cb4ff1a5e0e0b1af0ffb436a968608a1ee Mon Sep 17 00:00:00 2001 From: Hayden Williams Date: Mon, 29 Oct 2018 15:44:49 -0700 Subject: [PATCH 1/2] implement matrix convert to zero smethod --- lib/matrix_convert_to_zero.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/matrix_convert_to_zero.rb b/lib/matrix_convert_to_zero.rb index 4e7684b..f068dc3 100644 --- a/lib/matrix_convert_to_zero.rb +++ b/lib/matrix_convert_to_zero.rb @@ -3,5 +3,22 @@ # 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. def matrix_convert_to_0(matrix) - raise NotImplementedError + matrix.each do |row| + index = 0 + row.each do |element| + if element == 0 + row.length.times do |i| + row[i] = 0 + end + matrix.length.times do |i| + matrix[i][index] = 0 + end + end + + index += 1 + end + end end + +# the time complexity of this solution is O(n) where n is the number of elements in the matrix because the most temporally expensive part of the solution is the iteration of each element in the matrix. +# the space complexity is O(1) because the only space required in memory by this solution is a handful of iteration variables. All transformations are performed in place. From eb48b75ea232531ca8f69d92ef42e4f738a59383 Mon Sep 17 00:00:00 2001 From: Hayden Williams Date: Mon, 29 Oct 2018 17:53:40 -0700 Subject: [PATCH 2/2] fix time complexity --- lib/matrix_convert_to_zero.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matrix_convert_to_zero.rb b/lib/matrix_convert_to_zero.rb index f068dc3..4166532 100644 --- a/lib/matrix_convert_to_zero.rb +++ b/lib/matrix_convert_to_zero.rb @@ -20,5 +20,5 @@ def matrix_convert_to_0(matrix) end end -# the time complexity of this solution is O(n) where n is the number of elements in the matrix because the most temporally expensive part of the solution is the iteration of each element in the matrix. +# the time complexity of this solution is O(n * m) where n is the number of elements in each row and m is the number of elements in each column # the space complexity is O(1) because the only space required in memory by this solution is a handful of iteration variables. All transformations are performed in place.