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

Alice Hsiao - Matrix Convert to Zero #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

alicehsiao
Copy link

No description provided.

@shrutivanw
Copy link

Nice work!

The time complexity explanation is correct.
Imagine a scenario where all the values in your input matrix are zeroes. In this case, you'll end adding the index values of each cell into the zeros array. This means the space complexity of your algorithm is O(n*m) where n is the number of rows and m is the number of columns in the input parameter, matrix.

You could optimize the space complexity by instead tracking one array of size n to track the rows that need to be converted to 0s, and another array of size m to track the columns that need to be converted to 0s. Initialize them to false and make the corresponding ior jindex true in the row tracking matrix or column tracking matrix if matrix[i][j] is 0. This would reduce your space complexity to O(n+m) where n is the number of rows and m is the number of columns in the input parameter, matrix.

The only further optimization I could think of was to use the 0th row and the 0th column to track which columns and rows to convert to zero and make the space complexity O(1). Here's my solution: https://github.com/Ada-C10/matrix_convert_to_zero/blob/solution/lib/matrix_convert_to_zero.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants