Skip to content
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
31 changes: 23 additions & 8 deletions lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
# Updates the input matrix based on the following rules:
# Assumption/ Given: All numbers in the matrix are 0s or 1s
# 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: O(n*m), where n and m are nums of rows and columns
# Space complexity: O(n + m), where n and m are nums of rows and columns
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Space complexity looks better than n + m to me. 🙂

Any ideas on what it might be?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Oh, good question! So ... since this method doesn't create any extra structures to hold data, is it just constant? This was my first try at a matrix and I think I forgot that all it is is a single particularly organized array. O(n)?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'd agree with the former - if you wanted to be super-technically correct, you could say "constant additional space". (Excluding the size of your input and output variable(s) is a matter of convention).


# Time complexity: ?
# Space complexity: ?
def matrix_convert_to_zero(matrix)
raise NotImplementedError
end
return nil if matrix.empty?

rows = matrix.size
columns = matrix[0].size

rows.times do |row|
columns.times do |column|
matrix[row][column] = "ZERO" if matrix[row][column] == 0
end
end

rows.times do |row|
columns.times do |column|
next unless matrix[row][column] == "ZERO"

columns.times { |i| matrix[row][i] = 0 unless matrix[row][i] == "ZERO" }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Personal preference nit: some programmers (e.g. many Java people) might find this a bit too concise, and will prefer something more explicit.

Rubyists, unlike Java people, sometimes embrace this conciseness even if it makes code take slightly more time to understand - as it results in less code needing to be understood. 🙂

(Of course - like all personal preferences, this one's totally up to you.)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you! It's really helpful to hear about style preferences. When I work on this sort of thing in a vacuum, I can get totally drawn into the temptation to make it beauuuuuutifully elegant and forget about clarity/other people 😮 🙂

Copy link
Copy Markdown

@ace-n ace-n May 22, 2019

Choose a reason for hiding this comment

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

Yep - and style preferences depend heavily on the existing codebase. Many codebases/companies will have style guides, but inevitably your first few PRs at a new company (or even a new codebase within the same company! - e.g. iOS vs. Android) will be spent learning (and internalizing) that codebases' specific guidelines/patterns.

rows.times { |i| matrix[i][column] = 0 unless matrix[i][column] == "ZERO" }
matrix[row][column] = 0
end
end
end