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

Melissa : Matrix convert to zero #18

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

Conversation

melicious-dish
Copy link

No description provided.

@shrutivanw
Copy link

Nicely done! 👍

Since the number of rows, let's say n may not be equal to number of columns, let's say m, 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.

So, the time complexity of your algorithm will be O(n * m).

You can imagine a scenario where all the values in the input matrix are zero. In this scenario, the size of zeros_array in your algorithm will be n * m. You could optimize this by tracking the columns that you want to make zero in an array of size m and setting the value at index in that array to true if the column needs to be made zero. i.e.

  • change line 8 to zeros_array = Array.new(matrix[0].length, false)
  • change line 14 to zeros_array[index] = true
  • change lines 22 through 28 to:
  matrix.length.times do |row|
    matrix[0].length.times do |column|
      if zeros_array[column] == true
        matrix[row][column] = 0
      end
    end
  end
  return # since the matrix is modified in place, no need to return matrix

The only further optimization I could think of is to use row 0 and column 0 in the input matrix to track what needs to be converted to 0. Here's what I came up with: 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