Skip to content
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
16 changes: 16 additions & 0 deletions non-overlapping-intervals/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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


25 changes: 25 additions & 0 deletions remove-nth-node-from-end-of-list/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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


21 changes: 21 additions & 0 deletions same-tree/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -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))