From d493aa98a34987d575501362e25349d147698f94 Mon Sep 17 00:00:00 2001 From: leokim0922 Date: Fri, 2 Aug 2024 01:15:25 -0700 Subject: [PATCH] [Leo] 14th Week solutions --- set-matrix-zeroes/Leo.py | 25 +++++++++++++++++++++++++ spiral-matrix/Leo.py | 34 ++++++++++++++++++++++++++++++++++ sum-of-two-integers/Leo.py | 6 ++++++ 3 files changed, 65 insertions(+) create mode 100644 set-matrix-zeroes/Leo.py create mode 100644 spiral-matrix/Leo.py create mode 100644 sum-of-two-integers/Leo.py diff --git a/set-matrix-zeroes/Leo.py b/set-matrix-zeroes/Leo.py new file mode 100644 index 000000000..e4e36a3b9 --- /dev/null +++ b/set-matrix-zeroes/Leo.py @@ -0,0 +1,25 @@ +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + if not matrix: + return [] + + m = len(matrix) + n = len(matrix[0]) + + zeroes_row = [False] * m + zeroes_col = [False] * n + for row in range(m): + for col in range(n): + if matrix[row][col] == 0: + zeroes_row[row] = True + zeroes_col[col] = True + + for row in range(m): + for col in range(n): + if zeroes_row[row] or zeroes_col[col]: + matrix[row][col] = 0 + + ## TC: O(mn), SC: O(m+n) diff --git a/spiral-matrix/Leo.py b/spiral-matrix/Leo.py new file mode 100644 index 000000000..b3c9fab75 --- /dev/null +++ b/spiral-matrix/Leo.py @@ -0,0 +1,34 @@ +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + res = [] + while matrix: + res.extend(matrix.pop(0)) + matrix = [*zip(*matrix)][::-1] + return res + + ## TC: O(m * n), SC: O(m * n) + ## This sloution is kinda tricky and has higher SC than the below one + + # res = [] + # if len(matrix) == 0: + # return res + # row_begin = 0 + # col_begin = 0 + # row_end = len(matrix)-1 + # col_end = len(matrix[0])-1 + # while (row_begin <= row_end and col_begin <= col_end): + # for i in range(col_begin,col_end+1): + # res.append(matrix[row_begin][i]) + # row_begin += 1 + # for i in range(row_begin,row_end+1): + # res.append(matrix[i][col_end]) + # col_end -= 1 + # if (row_begin <= row_end): + # for i in range(col_end,col_begin-1,-1): + # res.append(matrix[row_end][i]) + # row_end -= 1 + # if (col_begin <= col_end): + # for i in range(row_end,row_begin-1,-1): + # res.append(matrix[i][col_begin]) + # col_begin += 1 + # return res diff --git a/sum-of-two-integers/Leo.py b/sum-of-two-integers/Leo.py new file mode 100644 index 000000000..fd7d38dca --- /dev/null +++ b/sum-of-two-integers/Leo.py @@ -0,0 +1,6 @@ +class Solution: + def getSum(self, a: int, b: int) -> int: + return add(a,b) + + ## I love this solution :P + ## TC: O(1), SC: O(1)