-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 천재네요... 배우고 갑니다 ㅋㅋ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 문제는 다른 분의 풀이를 참고했습니다....! |
||
|
||
return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.