diff --git a/set-matrix-zeroes/samthekorean.py b/set-matrix-zeroes/samthekorean.py new file mode 100644 index 000000000..f7ecfa779 --- /dev/null +++ b/set-matrix-zeroes/samthekorean.py @@ -0,0 +1,34 @@ +# TC: O(m * n) +# SC: O(1) +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 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 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