File tree Expand file tree Collapse file tree
non-overlapping-intervals
remove-nth-node-from-end-of-list Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # idea: -
2+ # Time Complexity: O(n log n)
3+ class Solution :
4+ def eraseOverlapIntervals (self , intervals : List [List [int ]]) -> int :
5+ intervals .sort ()
6+ cnt = 0
7+ prevEnd = intervals [0 ][1 ]
8+ for start , end in intervals [1 :]:
9+ if start >= prevEnd :
10+ prevEnd = end
11+ else :
12+ cnt += 1
13+ prevEnd = min (end , prevEnd )
14+ return cnt
15+
16+
Original file line number Diff line number Diff line change 1+ # Definition for singly-linked list.
2+ # class ListNode:
3+ # def __init__(self, val=0, next=None):
4+ # self.val = val
5+ # self.next = next
6+
7+ # idea : Two pointer
8+ # Time Complexity : O(length(list))
9+ class Solution :
10+ def removeNthFromEnd (self , head , n ):
11+ dummy = ListNode ()
12+ dummy .next = head
13+ fast = slow = dummy
14+
15+ for _ in range (n + 1 ):
16+ fast = fast .next
17+
18+ while fast :
19+ fast = fast .next
20+ slow = slow .next
21+
22+ slow .next = slow .next .next
23+ return dummy .next
24+
25+
Original file line number Diff line number Diff line change 1+ # Definition for a binary tree node.
2+ # class TreeNode:
3+ # def __init__(self, val=0, left=None, right=None):
4+ # self.val = val
5+ # self.left = left
6+ # self.right = right
7+
8+ # idea : DFS
9+ # Inorder method loses structural information, which is not suitable for it.
10+ # Time Complexity: O(p+q)
11+ class Solution :
12+ def isSameTree (self , p : Optional [TreeNode ], q : Optional [TreeNode ]) -> bool :
13+ if not p and not q :
14+ return True
15+ if not p or not q :
16+ return False
17+ if p .val != q .val :
18+ return False
19+
20+ return (self .isSameTree (p .left , q .left ) and self .isSameTree (p .right , q .right ))
21+
You can’t perform that action at this time.
0 commit comments