Skip to content

[Sam] Week 2 solutions #49

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 9 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions invert-binary-tree/SamTheKorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Time complexity : O(n)
# Space complexity : O(n)
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
stack = [root]

while stack:
node = stack.pop()

# Not accessing child if node is Null
if not node:
continue

node.left, node.right = node.right, node.left
Copy link
Member

Choose a reason for hiding this comment

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

ํŒŒ์ด์ฌ์€ ์ด๊ฒŒ ํ•œ ์ค„๋กœ ๋˜์„œ ๋„˜ ํŽธํ•ด์š” ใ…‹ใ…‹

stack += [node.left, node.right]

return root
15 changes: 15 additions & 0 deletions linked-list-cycle/SamTheKorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Time complexity : O(n)
# Space complexity : O(1)
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow = head
fast = head

# check fast and fast.next are not the last node to ensure we can access fast.next.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True

return False
32 changes: 32 additions & 0 deletions merge-two-sorted-lists/SamTheKorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Time complexity : O(m*n)
# Space complexity : O(1)
class Solution:
def mergeTwoLists(
self, list1: Optional[ListNode], list2: Optional[ListNode]
) -> Optional[ListNode]:
# Create a dummy node to simplify the logic
dummy = ListNode(None)
# Pointer to the current node in the merged list
node = dummy

# Loop until both lists have nodes
while list1 and list2:
# Choose the smaller value between list1 and list2
if list1.val < list2.val:
# Append list1 node to the merged list
node.next = list1
# Move to the next node in list1
list1 = list1.next
else:
# Append list2 node to the merged list
node.next = list2
# Move to the next node in list2
list2 = list2.next
# Move to the next node in the merged list
node = node.next

# Append the remaining nodes from list1 or list2
node.next = list1 or list2

# Return the merged list (skip the dummy node)
return dummy.next
27 changes: 27 additions & 0 deletions reverse-linked-list/SamTheKorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Time complexity : O(n)
# Space complexity : O(n)
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
# Return None when head is None
if not head:
return None

stack = []
Copy link
Member

Choose a reason for hiding this comment

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

Sam ๋‹˜์€ ์žฌ๊ท€๋ณด๋‹ค๋Š” ์Šคํƒ์“ฐ์‹œ๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•˜์‹œ๋Š” ๋“ฏ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

๋‹น์‹œ์— ์žฌ๊ท€ ๋ณด๋‹ค๋Š” ์Šคํƒ์ด ๋” ์ดํ•ด๊ฐ€ ํŽธํ–ˆ๋Š”๋ฐ ์ด์ œ ์กฐ๊ธˆ์”ฉ ์žฌ๊ท€์  ์‚ฌ๊ณ ์— ์ต์ˆ™ํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค

Copy link
Member

Choose a reason for hiding this comment

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

์ฐธ๊ณ ๋กœ ์ €๋Š” ๊ฐœ์ธ ์„ ํ˜ธ๋ฅผ ์กด์ค‘ํ•ด์š” ใ…‹ใ…‹ ๋ฌผ๋ก  ๋‘˜ ๋‹ค ์ž์œ ์ž์žฌ๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค๋ฉด ๋” ์ข‹๊ฒ ์ง€๋งŒ์š” ๐Ÿ˜ธ

node = head

while node:
stack.append(node)
node = node.next

head = stack.pop()
node = head

# End the loop when stack is empty
while stack:
node.next = stack.pop()
node = node.next

# Set the last node's next to None to indicate the end of the reversed list
node.next = None

return head
22 changes: 22 additions & 0 deletions valid-parentheses/SamTheKorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Time complexity : O(n)
# Space complexity : O(n)
class Solution:
def isValid(self, s: str) -> bool:
parentheses = {"[": "]", "{": "}", "(": ")"}
stack = []

# if ch is opening bracket
for ch in s:
if ch in parentheses:
stack.append(ch)
# if ch is closing bracket
else:
# if closing bracket comes without opening bracket previously
if not stack:
return False
# if closing bracket doesnt match with the latest type of opening bracket
if ch != parentheses[stack.pop()]:
return False

# True if stack is empty after going through the process above
return not stack