From 1bab0c4636ae7e9bc28b3e568d0279e4ec6d1fcc Mon Sep 17 00:00:00 2001 From: printjin Date: Sun, 15 Jun 2025 12:04:30 +0900 Subject: [PATCH 1/5] Solve : Same Tree --- same-tree/printjin-gmailcom.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 same-tree/printjin-gmailcom.py diff --git a/same-tree/printjin-gmailcom.py b/same-tree/printjin-gmailcom.py new file mode 100644 index 000000000..c9a451f77 --- /dev/null +++ b/same-tree/printjin-gmailcom.py @@ -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: + return False + return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) From 44381e92f31c3458da8dcd060ce515640358978b Mon Sep 17 00:00:00 2001 From: printjin Date: Thu, 19 Jun 2025 09:40:49 +0900 Subject: [PATCH 2/5] Solve : Remove Nth Node From End of List --- remove-nth-node-from-end-of-list/printjin-gmailcom.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 remove-nth-node-from-end-of-list/printjin-gmailcom.py diff --git a/remove-nth-node-from-end-of-list/printjin-gmailcom.py b/remove-nth-node-from-end-of-list/printjin-gmailcom.py new file mode 100644 index 000000000..854f68b8d --- /dev/null +++ b/remove-nth-node-from-end-of-list/printjin-gmailcom.py @@ -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 From aae3ae12c211bf6718f5e8d6d3c364f96dbeea26 Mon Sep 17 00:00:00 2001 From: printjin Date: Thu, 19 Jun 2025 09:43:00 +0900 Subject: [PATCH 3/5] Solve : Number of Connected Components in an Undirected Graph --- .../printjin-gmailcom.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 number-of-connected-components-in-an-undirected-graph/printjin-gmailcom.py diff --git a/number-of-connected-components-in-an-undirected-graph/printjin-gmailcom.py b/number-of-connected-components-in-an-undirected-graph/printjin-gmailcom.py new file mode 100644 index 000000000..a18a6a461 --- /dev/null +++ b/number-of-connected-components-in-an-undirected-graph/printjin-gmailcom.py @@ -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 From 28a55798a2813341b05c03d37bf10ddbcb89cd13 Mon Sep 17 00:00:00 2001 From: printjin Date: Thu, 19 Jun 2025 09:44:15 +0900 Subject: [PATCH 4/5] Solve : Non Overlapping Intervals --- non-overlapping-intervals/printjin-gmailcom.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 non-overlapping-intervals/printjin-gmailcom.py diff --git a/non-overlapping-intervals/printjin-gmailcom.py b/non-overlapping-intervals/printjin-gmailcom.py new file mode 100644 index 000000000..c1d8114bc --- /dev/null +++ b/non-overlapping-intervals/printjin-gmailcom.py @@ -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 From 13f26f292e5da17c7722eb654f8066a109e45e26 Mon Sep 17 00:00:00 2001 From: printjin Date: Thu, 19 Jun 2025 09:45:52 +0900 Subject: [PATCH 5/5] Solve : Serialize And Deserialize Binary Tree --- .../printjin-gmailcom.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 serialize-and-deserialize-binary-tree/printjin-gmailcom.py diff --git a/serialize-and-deserialize-binary-tree/printjin-gmailcom.py b/serialize-and-deserialize-binary-tree/printjin-gmailcom.py new file mode 100644 index 000000000..df79e56df --- /dev/null +++ b/serialize-and-deserialize-binary-tree/printjin-gmailcom.py @@ -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()