Skip to content

Latest commit

 

History

History
19 lines (11 loc) · 1.52 KB

File metadata and controls

19 lines (11 loc) · 1.52 KB

Time Complexity (Big O Time):

  1. The program first iterates through the matrix to determine whether the first row and first column need to be marked as zero (i.e., row0 and col0 flags).

  2. Then, it iterates through the entire matrix twice:

    • The first iteration (nested loops) is used to identify cells that need to be marked as zero based on the conditions. This loop runs for (m-1) * (n-1) iterations, where m is the number of rows, and n is the number of columns.
    • The second iteration (nested loops) is used to actually set the cells to zero based on the signals stored in the first row and first column. This loop also runs for (m-1) * (n-1) iterations.
  3. The program also sets the first row and first column to zero if necessary.

  4. Therefore, the overall time complexity of the program is O(m * n), where m is the number of rows, and n is the number of columns.

Space Complexity (Big O Space):

  1. The program uses only a few additional boolean variables (row0 and col0) and integer variables (loop counters). These variables consume constant space, which does not depend on the size of the input matrix.

  2. The program modifies the input matrix in-place without using any additional data structures. Therefore, the space complexity of the program is O(1) or constant space.

In summary, the time complexity of the provided program is O(m * n), and the space complexity is O(1). The program efficiently sets rows and columns of a matrix to zero based on specific conditions while using minimal extra space.