Skip to content

[SAM] WEEK 14 solutions, #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions set-matrix-zeroes/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions spiral-matrix/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +14 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

천재네요... 배우고 갑니다 ㅋㅋ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 문제는 다른 분의 풀이를 참고했습니다....!


return result
14 changes: 14 additions & 0 deletions sum-of-two-integers/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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