From a74d1468da923687be32c1cdb35ae210587b465f Mon Sep 17 00:00:00 2001 From: YOUNGJIN NA <120540450+ppxyn1@users.noreply.github.com> Date: Fri, 30 Jan 2026 17:21:20 +0900 Subject: [PATCH] [:solved] three problems --- non-overlapping-intervals/ppxyn1.py | 16 ++++++++++++++ remove-nth-node-from-end-of-list/ppxyn1.py | 25 ++++++++++++++++++++++ same-tree/ppxyn1.py | 21 ++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 non-overlapping-intervals/ppxyn1.py create mode 100644 remove-nth-node-from-end-of-list/ppxyn1.py create mode 100644 same-tree/ppxyn1.py diff --git a/non-overlapping-intervals/ppxyn1.py b/non-overlapping-intervals/ppxyn1.py new file mode 100644 index 0000000000..6914c6a4e4 --- /dev/null +++ b/non-overlapping-intervals/ppxyn1.py @@ -0,0 +1,16 @@ +# idea: - +# Time Complexity: O(n log n) +class Solution: + def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: + intervals.sort() + cnt = 0 + prevEnd = intervals[0][1] + for start, end in intervals[1:]: + if start >= prevEnd: + prevEnd = end + else: + cnt += 1 + prevEnd = min(end, prevEnd) + return cnt + + diff --git a/remove-nth-node-from-end-of-list/ppxyn1.py b/remove-nth-node-from-end-of-list/ppxyn1.py new file mode 100644 index 0000000000..e9e3fb2d14 --- /dev/null +++ b/remove-nth-node-from-end-of-list/ppxyn1.py @@ -0,0 +1,25 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next + +# idea : Two pointer +# Time Complexity : O(length(list)) +class Solution: + def removeNthFromEnd(self, head, n): + dummy = ListNode() + dummy.next = head + fast = slow = dummy + + for _ in range(n+1): + fast = fast.next + + while fast: + fast = fast.next + slow = slow.next + + slow.next = slow.next.next + return dummy.next + + diff --git a/same-tree/ppxyn1.py b/same-tree/ppxyn1.py new file mode 100644 index 0000000000..c795002efd --- /dev/null +++ b/same-tree/ppxyn1.py @@ -0,0 +1,21 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right + +# idea : DFS +# Inorder method loses structural information, which is not suitable for it. +# Time Complexity: O(p+q) +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)) +