Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 24 additions & 0 deletions binary-tree-level-order-traversal/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# TC : O(n)
# SC : O(n)
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
q = collections.deque()
res = []
q.append(root)

while q:
level = []
qLen = len(q)

# traverse nodes in each level and append left and right subtree if node is None
for i in range(qLen):
node = q.popleft()
if node:
level.append(node.val)
# append the subtrees of q for the next level in advance
q.append(node.left)
q.append(node.right)
if level:
res.append(level)

return res
16 changes: 16 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TC : O(log n) because its a balanced tree
# SC : O(1)
class Solution:
def lowestCommonAncestor(
self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
) -> "TreeNode":
current = root

# Return when they split to left and right
while current:
if p.val > current.val and q.val > current.val:
current = current.right
elif p.val < current.val and q.val < current.val:
current = current.left
else:
return current
Comment on lines +10 to +16
Copy link
Member

Choose a reason for hiding this comment

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

์ด ๋ฌธ์ œ๊ฐ€ ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํžˆ ํ’€์ด๋  ์ˆ˜ ์žˆ๋Š” ๊ฑฐ์˜€๊ตฐ์š”.. ๋ฐ”๋กœ ์ฝ”๋“œ ์ˆ˜์ •ํ•˜๋Ÿฌ ๊ฐ‘๋‹ˆ๋‹ค ๐Ÿ’จ

23 changes: 23 additions & 0 deletions remove-nth-node-from-end-of-list/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# TC : O(n)
# SC : O(1)
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
# Create a dummy node to handle edge cases such as removing the first node
dummy = ListNode(0, head)
left = dummy
right = head

# Move the right pointer n steps ahead
for _ in range(n):
right = right.next

# Move both pointers until right reaches the end
while right:
left = left.next
right = right.next

# Remove the nth node from the end
left.next = left.next.next

# Return the head of the modified list
return dummy.next
32 changes: 32 additions & 0 deletions reorder-list/samthekorean.py
Copy link
Contributor

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.

๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ํšจ์œจ์ ์œผ๋กœ ์“ธ ์ˆ˜ ์žˆ๋˜๋ฐ ์‹œ๊ฐ„์ด ๋‚˜๋ฉด ๋‹ค์‹œ ์งœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

Copy link
Member

Choose a reason for hiding this comment

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

์•— ๋ฆฌ๋ทฐ๋ฅผ ๋นผ์•—๊ฒผ๋„ค์š” ๐Ÿคฃ
deque์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๋„ค์š”!

Copy link
Contributor Author

@SamTheKorean SamTheKorean Jun 11, 2024

Choose a reason for hiding this comment

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

์‚ฌ์‹ค deque๋ฅผ ์ž˜ ๋ชฐ๋ž์—ˆ๋Š”๋ฐ chatgpt์— stack์˜ ์•„๋žซ๋ถ€๋ถ„๊ณผ ์œ—๋ถ€๋ถ„์„ ๋™์‹œ์— popํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๋ฌผ์–ด๋ณด๋‹ˆ deque๊ฐ€ ๊ทธ๋Ÿฐ ์ž๋ฃŒ๊ตฌ์กฐ๋ผ๊ณ  ํ•ด์„œ ์ฒ˜์Œ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฐ๋ฐ ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์€ ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋„ค์šฉ

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# TC : O(n)
# SC : O(m) m being the sum of deque and dummy nodes
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
if not head or not head.next or not head.next.next:
return

# Step 1: Use a deque to collect nodes
d = deque()
current = head
while current:
d.append(current)
current = current.next

# Step 2: Reorder the list using deque
dummy = ListNode(0) # Dummy node with value 0
current = dummy
toggle = True

while d:
if toggle:
current.next = d.popleft() # Append from the front of the deque
else:
current.next = d.pop() # Append from the back of the deque
current = current.next
toggle = not toggle

# Step 3: Ensure the last node points to None
current.next = None

# Step 4: Reassign head to point to the new reordered list
head = dummy.next