-
Notifications
You must be signed in to change notification settings - Fork 37
Ports - Kate #16
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
base: master
Are you sure you want to change the base?
Ports - Kate #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
| # 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" } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😮 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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 + mto me. 🙂Any ideas on what it might be?
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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).