Skip to content

[printjin-gmailcom] week 12 solutions #1584

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 5 commits into from
Jun 19, 2025
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
13 changes: 13 additions & 0 deletions non-overlapping-intervals/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import List

class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
intervals.sort(key=lambda x: x[1])
count = 0
end = float('-inf')
for start, finish in intervals:
if start < end:
count += 1
else:
end = finish
return count
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import (
List,
)

class Solution:
def count_components(self, n: int, edges: List[List[int]]) -> int:
parent = [i for i in range(n)]
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
for a, b in edges:
root_a = find(a)
root_b = find(b)
if root_a != root_b:
parent[root_a] = root_b
n -= 1
return n
11 changes: 11 additions & 0 deletions remove-nth-node-from-end-of-list/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def removeNthFromEnd(self, head, n):
dummy = ListNode(0, head)
fast = slow = dummy
for _ in range(n):
fast = fast.next
while fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next
9 changes: 9 additions & 0 deletions same-tree/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p and not q:
return True
if not p or not q:
return False
if p.val != q.val:
Copy link
Contributor

Choose a reason for hiding this comment

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

if not p or not q or p.val != q.val:
    return False

의 형태로 조건문을 더욱 간략하게 줄일수도 있을것 같습니다!
(워낙 간결한 코드라, 이정도 코멘트밖에 드릴게 없네요.. 🤣)

return False
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
26 changes: 26 additions & 0 deletions serialize-and-deserialize-binary-tree/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Codec:
def serialize(self, root):
def dfs(node):
if not node:
vals.append("None")
return
vals.append(str(node.val))
dfs(node.left)
dfs(node.right)

vals = []
dfs(root)
return ",".join(vals)

def deserialize(self, data):
def dfs():
val = next(vals)
if val == "None":
return None
node = TreeNode(int(val))
node.left = dfs()
node.right = dfs()
return node

vals = iter(data.split(","))
return dfs()