0
\$\begingroup\$

I've solved LC medium problem, set zeros. Below code is accepted and all test cases were passing. But my code came in the last 10% of accepted answers wrt performance. Can someone help me identify optimization opportunities? This is in C# and memory footprint is fine.

public class Solution {
    public void SetZeroes(int[][] matrix) {
            
            bool zeroRow = false;
            bool zeroCol = false;

            for (int i = 0;i<matrix.Length;++i)
            {
                for (int j = 0; j < matrix[0].Length; ++j)
                {
                    if (matrix[i][j] == 0)
                    {
                        if (i == 0)
                            zeroRow = true;
                        if (j == 0)
                            zeroCol = true;

                        matrix[i][0] = 0;
                        matrix[0][j] = 0;
                    }
                 
                }
                
            }

            for (int i = 1; i < matrix.Length; ++i)
            {
                for (int j = 1; j < matrix[0].Length; ++j)
                {
                    if (matrix[i][0] == 0 || matrix[0][j] == 0)
                    {
                        matrix[i][j] = 0;                        
                    }

                }

            }



            Console.Write('[');
            for (int i = 0; i < matrix.Length; i++)
            {
                if (zeroCol) { matrix[i][0] = 0; }
                Console.Write('[');
                for (int j = 0; j < matrix[0].Length; j++)
                {
                    if (zeroRow) { matrix[0][j] = 0; }
                    Console.Write(matrix[i][j]);
                    if(j!=matrix[0].Length-1)
                    Console.Write(',');
                }
                Console.Write("]");
                if (i != matrix.Length-1)
                    Console.Write(',');
            }
            Console.Write(']');
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. I'm afraid "set zeros" means pretty much nothing as a program requirement - can you be more specific? Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. \$\endgroup\$ – Toby Speight Feb 27 at 17:44
  • 2
    \$\begingroup\$ Are you talking about the following problem? leetcode.com/problems/set-matrix-zeroes if so, it looks like the matrix should be updated in memory so I am not sure why you need the Console.Write (that might affect performance) \$\endgroup\$ – Yumei De Armas Feb 27 at 21:35
  • \$\begingroup\$ @YumeiDeArmas i think you might be right, let me try. \$\endgroup\$ – rplusg Feb 28 at 5:29
  • \$\begingroup\$ @YumeiDeArmas That is the problem, thank you. \$\endgroup\$ – rplusg Feb 28 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.