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

Pushpa_Agr_matrix_convert_to_0 #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pushpaagr
Copy link

time complexity 2 n^2 as there are 2 nested loops.
space complexity linear as it will depend on the size row/column of how many zeros there are.

@shrutivanw
Copy link

Nice work!

Let's say the number of rows are n and the number of columns are m. Since the number of rows may not be equal to number of columns, speak to the time and space complexities in terms n and m instead of in terms of just n. Also, always describe what n and m stand for while explaining complexities in terms of n and m.

So, in this case, your algorithm's time complexity will be O(n*m) where n is the number of rows and m is the number of columns.

You can imagine a scenario where the whole input matrix has just 0s. In this case, rows as well as columns will both be n m long and hence your algorithm's space complexity with be O(nm) where n is the number of rows and m is the number of columns.
You could optimize the space complexity further by making the rows array n long and the columns array m long with all values initialized to false. Then, change lines 21 and 22 to:

   columns[j] = true
   rows[i] = true

And use this information later in the algorithm to convert the corresponding row or column to zero. This would improve the algorithm's complexity to O(n+m).

As always, slack me if you have further questions or want to chat further.

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