forked from AdaGold/matrix-convert-to-zero
-
Notifications
You must be signed in to change notification settings - Fork 37
Sockets - Jansen #8
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
Open
JansenMartin
wants to merge
1
commit into
Ada-C11:master
Choose a base branch
from
JansenMartin:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,71 @@ | |
| # 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: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(r * c), where r equals the number of arrays (rows) inside the array, and | ||
| # c equals the number of elements (columns) inside of a nested array | ||
| # Space complexity: O(1), or constant. We're not creating any additional data structures that hinges on user input | ||
| def matrix_convert_to_zero(matrix) | ||
| raise NotImplementedError | ||
| # Check for edge cases | ||
| return if (matrix == nil || matrix == []) | ||
|
|
||
| # Determine length of rows and columns | ||
| rows = matrix.length | ||
| columns = matrix[0].length | ||
|
|
||
| row_zero_zeroes = false | ||
| # Does Row 0 have zeros? | ||
| columns.times do |col| | ||
| if matrix[0][col] == 0 | ||
| row_zero_zeroes = true | ||
| end | ||
| end | ||
|
|
||
| column_zero_zeroes = false | ||
|
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. I'd recommend having all of your variables at the top of your method, that way if you ever need to add or change a variable you do not need to hunt through the method to find it. |
||
| # Does Column 0 have zeros? | ||
| rows.times do |row| | ||
| if matrix[row][0] == 0 | ||
| column_zero_zeroes = true | ||
| end | ||
| end | ||
|
|
||
| # Check (Rows 1 - n) and (Cols 1 - n) | ||
| # Track found zeros through Row 0 and Col 0 | ||
| # (by changing corresponding element into 0) | ||
|
|
||
| (rows - 1).times do |row| | ||
| (columns - 1).times do |col| | ||
| if matrix[row + 1][col + 1] == 0 | ||
| matrix[0][col + 1] = 0 | ||
| matrix[row + 1][0] = 0 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Look at an element inside matrix | ||
| # Check corresponding element inside row 0 or col 0 | ||
| # Are either 0? If so, change targeted element to 0 | ||
|
|
||
| (rows - 1).times do |row| | ||
| (columns - 1).times do |col| | ||
| if matrix[0][col + 1] == 0 || matrix[row + 1][0] == 0 | ||
| matrix[row + 1][col + 1] = 0 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # IF Row 0 has zeros: Populate with 0 | ||
| if row_zero_zeroes | ||
| columns.times do |col| | ||
| matrix[0][col] = 0 | ||
| end | ||
| end | ||
|
|
||
| # IF Col 0 has zeros: Populate with 0 | ||
| if column_zero_zeroes | ||
| rows.times do |row| | ||
| matrix[row][0] = 0 | ||
| end | ||
| end | ||
|
|
||
| return matrix | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Great explanations on time and space complexity.