Skip to content

Commit 162f392

Browse files
authored
Merge pull request #2303 from doh6077/main
[doh6077] WEEK 12 solutions
2 parents 64da445 + b7f1914 commit 162f392

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution:
2+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
3+
4+
# # first structure it as graph (dictionary)
5+
# intervals = [[1,2],[2,3],[3,4],[1,3]]
6+
# dict = {}
7+
# for i in intervals:
8+
# dict[i[0]] = []
9+
# for a,b in intervals:
10+
# dict[a].append(b)
11+
12+
# #Overlapping edge cases
13+
# # 1. [1,4], [2,3] -> not sure how to figure out
14+
# # 2. [1,3], [1,2] , [3,4] -> check if the value has connection
15+
# # 3. [1,2], [1,2], [1,2] ( same values) use hashset
16+
17+
# count = 0
18+
# visited = set()
19+
# for key, value in dict.items():
20+
# if value not in visited:
21+
# visited.append(value)
22+
# else:
23+
# count += 1
24+
# return count
25+
26+
# count = 0
27+
# visited = set()
28+
# for a, b in intervals:
29+
# if b not in visited:
30+
# visited.add(b)
31+
# else:
32+
# count += 1
33+
# return count
34+
35+
36+
# greedy Approach
37+
intervals.sort()
38+
39+
res = 0
40+
prevEnd = intervals[0][1]
41+
for start, end in intervals[1:]:
42+
if start >= prevEnd:
43+
prevEnd = end
44+
else:
45+
res += 1
46+
prevEnd = min(prevEnd, end)
47+
return res
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
# 19. Remove Nth Node From End of List
3+
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
4+
dummy = ListNode(0, head)
5+
6+
# Calculate Length
7+
length = 0
8+
current = head
9+
while current:
10+
length += 1
11+
current = current.next
12+
13+
stop_index = length - n
14+
15+
current = dummy
16+
for _ in range(stop_index):
17+
current = current.next
18+
19+
# Delete the node
20+
current.next = current.next.next
21+
22+
# Return the start of the list (dummy.next handles if head changed)
23+
return dummy.next

same-tree/doh6077.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# 100. Same Tree
3+
# https://leetcode.com/problems/same-tree/description/
4+
5+
6+
# Definition for a binary tree node.
7+
# class TreeNode:
8+
# def __init__(self, val=0, left=None, right=None):
9+
# self.val = val
10+
# self.left = left
11+
# self.right = right
12+
class Solution:
13+
#Time Complexity: O(N)- it visits every node once
14+
#Space Complexity: O(H)- H is the height of the tree, which is the maximum depth of the recursion stack
15+
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
16+
# DFS approach
17+
def dfs(node1, node2):
18+
if node1 is None and node2 is None:
19+
return True
20+
if node1 is None or node2 is None:
21+
return False
22+
if node1.val != node2.val:
23+
return False
24+
return dfs(node1.left,node2.left) and dfs(node1.right,node2.right)
25+
26+
return dfs(p, q)

0 commit comments

Comments
 (0)