From 7d61dc3aae0d5e6679821379ab108821db080d52 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Fri, 2 Aug 2024 15:57:10 -0400 Subject: [PATCH 1/4] solve : spiral-matrix/samthekorean.py --- spiral-matrix/samthekorean.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spiral-matrix/samthekorean.py diff --git a/spiral-matrix/samthekorean.py b/spiral-matrix/samthekorean.py new file mode 100644 index 000000000..cd214fe47 --- /dev/null +++ b/spiral-matrix/samthekorean.py @@ -0,0 +1,27 @@ +# TC: O(m * n) +# SC: O(m * n) +# where m is the number of the row and n is the number of columns +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + rows, cols = len(matrix), len(matrix[0]) + + row, col = 0, -1 + + direction = 1 + + result = [] + + while rows > 0 and cols > 0: + for _ in range(cols): + col += direction + result.append(matrix[row][col]) + rows -= 1 + + for _ in range(rows): + row += direction + result.append(matrix[row][col]) + cols -= 1 + + direction *= -1 + + return result From 82d9503c4e1cccaefdc8fba9511e0a5154ca59fa Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Fri, 2 Aug 2024 20:29:43 -0400 Subject: [PATCH 2/4] solve : set matrix zeros --- set-matrix-zeroes/samthekorean.py | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 set-matrix-zeroes/samthekorean.py diff --git a/set-matrix-zeroes/samthekorean.py b/set-matrix-zeroes/samthekorean.py new file mode 100644 index 000000000..06af9634e --- /dev/null +++ b/set-matrix-zeroes/samthekorean.py @@ -0,0 +1,34 @@ +# TC: O(m * n) +# SC: O(m * n) +class Solution: + def setZeroes(self, matrix: List[List[int]]) -> None: + rows, cols = len(matrix), len(matrix[0]) + first_row_zero = any(matrix[0][j] == 0 for j in range(cols)) + first_col_zero = any(matrix[i][0] == 0 for i in range(rows)) + + # Use first row and column to mark zero rows and columns + for i in range(1, rows): + for j in range(1, cols): + if matrix[i][j] == 0: + matrix[i][0] = matrix[0][j] = 0 + + # Set rows to zero based on marks in the first column + for i in range(1, rows): + if matrix[i][0] == 0: + for j in range(1, cols): + matrix[i][j] = 0 + + # Set columns to zero based on marks in the first row + for j in range(1, cols): + if matrix[0][j] == 0: + for i in range(1, rows): + matrix[i][j] = 0 + + # Handle the first row and column separately if needed + if first_row_zero: + for j in range(cols): + matrix[0][j] = 0 + + if first_col_zero: + for i in range(rows): + matrix[i][0] = 0 From 72f59ed596d2c7a592c577b4bc6361a5da2241a2 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Sat, 3 Aug 2024 01:12:04 -0400 Subject: [PATCH 3/4] solve : sum of the integers --- sum-of-two-integers/samthekorean.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 sum-of-two-integers/samthekorean.py diff --git a/sum-of-two-integers/samthekorean.py b/sum-of-two-integers/samthekorean.py new file mode 100644 index 000000000..aee7a6450 --- /dev/null +++ b/sum-of-two-integers/samthekorean.py @@ -0,0 +1,14 @@ +# TC : O(n) +# SC : O(n) +class Solution: + def getSum(self, a: int, b: int) -> int: + + mask = 0xFFFFFFFF + + while (b & mask) > 0: + + carry = (a & b) << 1 + a = a ^ b + b = carry + + return (a & mask) if b > 0 else a From 0549b6eeb9a05d7acaa2ada56e85d92e089ea559 Mon Sep 17 00:00:00 2001 From: SamTheKorean Date: Sat, 3 Aug 2024 19:00:41 -0400 Subject: [PATCH 4/4] fix : change time complexity --- set-matrix-zeroes/samthekorean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/set-matrix-zeroes/samthekorean.py b/set-matrix-zeroes/samthekorean.py index 06af9634e..f7ecfa779 100644 --- a/set-matrix-zeroes/samthekorean.py +++ b/set-matrix-zeroes/samthekorean.py @@ -1,5 +1,5 @@ # TC: O(m * n) -# SC: O(m * n) +# SC: O(1) class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: rows, cols = len(matrix), len(matrix[0])