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

Kay matrix-check-sum #15

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
26 changes: 25 additions & 1 deletion lib/matrix_check_sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
# whether the sum of each row matches the sum of corresponding column i.e. sum
# of numbers in row i is the same as the sum of numbers in column i for i = 0 to row.length-1
# If this is the case, return true. Otherwise, return false.
require 'pry'
def matrix_check_sum(matrix)
raise NotImplementedError
row_sum = 0
column_sum = 0

matrix.length.times do |i| # the # of rows there are
matrix[i].length.times do |j| # the # of columns in this row
row_sum += matrix[i][j] #row 0 column 0, row 0 column 1, row 0 column 2
column_sum += matrix[j][i]
end
if row_sum != column_sum
return false
end
end
return true
end

#Space Complexity:
# O(n + m) where n is the length/size of the row, and m is length/size of the column.

Choose a reason for hiding this comment

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

The two variables you defined will take up space. The value of those variables will change, but the size they'll take up will remain the same through their lifetime. So, the space complexity will be constant or O(1).

# Two variables are defined, and the space that their values take up will increase
# based on the row + column value.
#
#Time Complexity:
# O(n * m) where n is the length of the row, and m is the length of the column.
# There are two loops in this solution. In the worst case the first will run
# n times and the second will run m times.
#