From 4e177363e86ef449cecc79a5630d56cac1c36304 Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Tue, 2 Nov 2021 03:07:33 +0900 Subject: [PATCH] =?UTF-8?q?feat(leetcode/medium/73-set-matrix-zeroes.py)?= =?UTF-8?q?=C2=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../73-set-matrix-zeroes.py | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 coding-challange/leetcode/medium/73-set-matrix-zeroes/73-set-matrix-zeroes.py diff --git a/coding-challange/leetcode/medium/73-set-matrix-zeroes/73-set-matrix-zeroes.py b/coding-challange/leetcode/medium/73-set-matrix-zeroes/73-set-matrix-zeroes.py new file mode 100644 index 00000000..b8ae8cb0 --- /dev/null +++ b/coding-challange/leetcode/medium/73-set-matrix-zeroes/73-set-matrix-zeroes.py @@ -0,0 +1,65 @@ +""" +73-set-matrix-zeroes +leetcode/medium/73. Set Matrix Zeroes +Difficulty: medium +URL: https://leetcode.com/problems/set-matrix-zeroes/ +""" + +from typing import List + + +class Solution: + def replace_to_zero(self, matrix, row_index, column_index, direction): + + if direction == "up": + matrix[row_index][column_index] = 0 + if row_index > 0: + self.replace_to_zero(matrix, row_index - 1, column_index, direction) + + if direction == "down": + matrix[row_index][column_index] = 0 + if row_index < len(matrix) - 1: + self.replace_to_zero(matrix, row_index + 1, column_index, direction) + + if direction == "left": + matrix[row_index][column_index] = 0 + if column_index > 0: + self.replace_to_zero(matrix, row_index, column_index - 1, direction) + + if direction == "right": + matrix[row_index][column_index] = 0 + if column_index < len(matrix[0]) - 1: + self.replace_to_zero(matrix, row_index, column_index + 1, direction) + + def setZeroes(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + queue = [] + + for row_index in range(len(matrix)): + for column_index in range(len(matrix[0])): + if matrix[row_index][column_index] == 0: + queue.append([row_index, column_index]) + + for [row_index, column_index] in queue: + self.replace_to_zero(matrix, row_index, column_index, "up") + self.replace_to_zero(matrix, row_index, column_index, "down") + self.replace_to_zero(matrix, row_index, column_index, "left") + self.replace_to_zero(matrix, row_index, column_index, "right") + + +def test(): + matrix = [ + [0, 1, 2, 0], + [3, 4, 5, 2], + [1, 3, 1, 5] + ] + output = [ + [0, 0, 0, 0], + [0, 4, 5, 0], + [0, 3, 1, 0], + ] + Solution().setZeroes(matrix) + + assert matrix == output