|
| 1 | +""" |
| 2 | +Python version 3.7.0 |
| 3 | +1.8 - Zero Matrix |
| 4 | +Write an algorithm such that if an element in an MxN matrix is 0, |
| 5 | +its entire row and column are set to 0 |
| 6 | +""" |
| 7 | +import unittest |
| 8 | +from typing import List |
| 9 | + |
| 10 | + |
| 11 | +def zero_matrix(matrix: List[List[int]]) -> List[List[int]]: |
| 12 | + """ |
| 13 | + zero_matrix will take in an MxN matrix and when a 0 element is found, |
| 14 | + the whole column and row will be set to 0. |
| 15 | + The algorithm works by first scanning for |
| 16 | + rows and columns where there is a 0 and store into a set. |
| 17 | + Then we go through each stored row and column indices |
| 18 | + and proceed to set the rows and columns to 0. |
| 19 | + Runtime: O(M * N) |
| 20 | + Space Complexity: O(M + N) |
| 21 | + :param matrix: an MxN matrix. M is the number of rows, N is the number of columns |
| 22 | + :return: a zero-modified matrix |
| 23 | + """ |
| 24 | + M = len(matrix) |
| 25 | + N = len(matrix[0]) |
| 26 | + |
| 27 | + rows_to_zero = set() |
| 28 | + cols_to_zero = set() |
| 29 | + # first, scan for coordinates of 0s |
| 30 | + for i, row in enumerate(matrix): |
| 31 | + for j, num in enumerate(row): |
| 32 | + if num != 0: |
| 33 | + continue |
| 34 | + # otherwise, save row or col to rows or cols to zero set |
| 35 | + rows_to_zero.add(i) |
| 36 | + cols_to_zero.add(j) |
| 37 | + for i in rows_to_zero: |
| 38 | + # set row to 0 by looping through columns of current row |
| 39 | + for k in range(N): |
| 40 | + matrix[i][k] = 0 |
| 41 | + |
| 42 | + for j in cols_to_zero: |
| 43 | + # set column to 0 by looping through rows of current column |
| 44 | + for l in range(M): |
| 45 | + matrix[l][j] = 0 |
| 46 | + return matrix |
| 47 | + |
| 48 | + |
| 49 | +class TestZeroMatrixFunction(unittest.TestCase): |
| 50 | + |
| 51 | + def setUp(self): |
| 52 | + self.cases = [ |
| 53 | + ( |
| 54 | + [ |
| 55 | + [1, 2, 3, 4], |
| 56 | + [5, 6, 0, 8], |
| 57 | + [9, 10, 11, 12], |
| 58 | + [13, 14, 15, 16] |
| 59 | + ], |
| 60 | + [ |
| 61 | + [1, 2, 0, 4], |
| 62 | + [0, 0, 0, 0], |
| 63 | + [9, 10, 0, 12], |
| 64 | + [13, 14, 0, 16] |
| 65 | + ] |
| 66 | + ), |
| 67 | + ( |
| 68 | + [ |
| 69 | + [1, 2], |
| 70 | + [3, 4], |
| 71 | + [5, 6], |
| 72 | + [7, 8], |
| 73 | + [9, 0] |
| 74 | + ], |
| 75 | + [ |
| 76 | + [1, 0], |
| 77 | + [3, 0], |
| 78 | + [5, 0], |
| 79 | + [7, 0], |
| 80 | + [0, 0] |
| 81 | + ], |
| 82 | + ), |
| 83 | + ( |
| 84 | + [ |
| 85 | + [1, 2], |
| 86 | + [0, 4], |
| 87 | + [5, 6], |
| 88 | + [7, 8], |
| 89 | + [9, 0] |
| 90 | + ], |
| 91 | + [ |
| 92 | + [0, 0], |
| 93 | + [0, 0], |
| 94 | + [0, 0], |
| 95 | + [0, 0], |
| 96 | + [0, 0] |
| 97 | + ] |
| 98 | + ), |
| 99 | + ( |
| 100 | + [ |
| 101 | + [1, 2, 3, 4, 5, 6, 0, 8, 9], |
| 102 | + [9, 8, 7, 6, 5, 4, 3, 2, 1], |
| 103 | + ], |
| 104 | + [ |
| 105 | + [0, 0, 0, 0, 0, 0, 0, 0, 0], |
| 106 | + [9, 8, 7, 6, 5, 4, 0, 2, 1], |
| 107 | + ] |
| 108 | + ), |
| 109 | + ( |
| 110 | + [ |
| 111 | + [0] |
| 112 | + ], |
| 113 | + [ |
| 114 | + [0] |
| 115 | + ] |
| 116 | + ), |
| 117 | + ( |
| 118 | + [ |
| 119 | + [1] |
| 120 | + ], |
| 121 | + [ |
| 122 | + [1] |
| 123 | + ] |
| 124 | + ), |
| 125 | + ( |
| 126 | + [ |
| 127 | + [1, 2], |
| 128 | + [3, 4] |
| 129 | + ], |
| 130 | + [ |
| 131 | + [1, 2], |
| 132 | + [3, 4] |
| 133 | + ] |
| 134 | + ), |
| 135 | + ( |
| 136 | + [ |
| 137 | + [0, 1, 2], |
| 138 | + [0, 3, 4], |
| 139 | + [0, 5, 6], |
| 140 | + [0, 7, 8] |
| 141 | + ], |
| 142 | + [ |
| 143 | + [0, 0, 0], |
| 144 | + [0, 0, 0], |
| 145 | + [0, 0, 0], |
| 146 | + [0, 0, 0] |
| 147 | + ] |
| 148 | + ), |
| 149 | + ( |
| 150 | + [ |
| 151 | + [1, 2, 3, 4], |
| 152 | + [5, 6, 0, 8], |
| 153 | + [9, 10, 0, 12], |
| 154 | + [13, 14, 15, 16] |
| 155 | + ], |
| 156 | + [ |
| 157 | + [1, 2, 0, 4], |
| 158 | + [0, 0, 0, 0], |
| 159 | + [0, 0, 0, 0], |
| 160 | + [13, 14, 0, 16] |
| 161 | + ] |
| 162 | + ), |
| 163 | + ( |
| 164 | + [ |
| 165 | + [0, 0, 0, 0], |
| 166 | + [1, 2, 3, 4], |
| 167 | + [5, 6, 7, 8], |
| 168 | + [9, 10, 11, 12] |
| 169 | + ], |
| 170 | + [ |
| 171 | + [0, 0, 0, 0], |
| 172 | + [0, 0, 0, 0], |
| 173 | + [0, 0, 0, 0], |
| 174 | + [0, 0, 0, 0] |
| 175 | + ] |
| 176 | + ), |
| 177 | + ( |
| 178 | + [[0] * 1000 for _ in range(1000)], |
| 179 | + [[0] * 1000 for _ in range(1000)], |
| 180 | + ) |
| 181 | + ] |
| 182 | + |
| 183 | + def test_zero_matrix(self): |
| 184 | + for matrix, expected in self.cases: |
| 185 | + self.assertEqual(zero_matrix(matrix), expected, msg=(matrix, expected)) |
| 186 | + |
| 187 | + |
| 188 | +if __name__ == '__main__': |
| 189 | + unittest.main() |
0 commit comments