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

Hayden - Edges (C10) - Matrix CheckSum #10

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
27 changes: 26 additions & 1 deletion lib/matrix_check_sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,30 @@
# 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.
def matrix_check_sum(matrix)
raise NotImplementedError
per_line = matrix.length
col_index = 0

matrix.each do |row|
row_sum = 0
per_line.times do |i|
row_sum += row[i]
end

col_sum = 0
per_line.times do |i|
col_sum += matrix[i][col_index]
end

if row_sum == col_sum
col_index += 1
else
return false
end

end
return true
end


# the time complexity for this solution is O(n^2) because for each element in a column, it also has to iterate over each element in the corresponding row.
# the space complexity is O(1) because the only memory required is a few iteration variables and the accumulators