Skip to content

Commit 14b4e50

Browse files
authored
Merge pull request DaleStudy#162 from SamTheKorean/main
[SAM] Week 10 solutions
2 parents dcdf35f + 18947f7 commit 14b4e50

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

graph-valid-tree/samthekorean.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# O(n+e) where n is number of nodes and e is the number of edges.
2+
# O(n+e) where n is number of nodes and e is the number of edges.
3+
class Solution:
4+
def validTree(self, numNodes: int, connections: List[List[int]]) -> bool:
5+
adjacencyList = [[] for _ in range(numNodes)]
6+
for src, dst in connections:
7+
adjacencyList[src].append(dst)
8+
adjacencyList[dst].append(src)
9+
10+
visitedNodes = set()
11+
12+
def detectCycle(currentNode, previousNode):
13+
if currentNode in visitedNodes:
14+
return True
15+
visitedNodes.add(currentNode)
16+
for neighbor in adjacencyList[currentNode]:
17+
if neighbor == previousNode:
18+
continue
19+
if detectCycle(neighbor, currentNode):
20+
return True
21+
return False
22+
23+
if detectCycle(0, -1):
24+
return False
25+
return len(visitedNodes) == numNodes

house-robber-ii/samthekorean.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# TC : O(n), where n is the number of houses.
2+
# SC : O(1)
3+
class Solution:
4+
def rob(self, nums):
5+
n = len(nums)
6+
if n == 1:
7+
return nums[0]
8+
if n == 2:
9+
return max(nums[0], nums[1])
10+
if n == 3:
11+
return max(nums[0], max(nums[1], nums[2]))
12+
13+
a = nums[0]
14+
b = max(nums[0], nums[1])
15+
c = -1
16+
a1 = nums[1]
17+
b1 = max(nums[1], nums[2])
18+
c1 = -1
19+
20+
for i in range(2, n):
21+
if i < n - 1:
22+
c = max(nums[i] + a, b)
23+
a = b
24+
b = c
25+
if i > 2:
26+
c1 = max(nums[i] + a1, b1)
27+
a1 = b1
28+
b1 = c1
29+
30+
return max(c, c1)

house-robber/samthekorean.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# TC : O(n)
2+
# SC : O(n)
3+
class Solution:
4+
def rob(self, nums):
5+
n = len(nums)
6+
if n == 0:
7+
return 0
8+
if n == 1:
9+
return nums[0]
10+
11+
dp = [-1] * n
12+
dp[0] = nums[0]
13+
if n > 1:
14+
dp[1] = max(nums[0], nums[1])
15+
16+
for i in range(2, n):
17+
dp[i] = max(dp[i - 1], nums[i] + dp[i - 2])
18+
19+
return dp[-1]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# O(n^2)
2+
# O(1)
3+
class Solution:
4+
def longestPalindrome(self, s: str) -> str:
5+
if len(s) <= 1:
6+
return s
7+
8+
def expand_from_center(left, right):
9+
while left >= 0 and right < len(s) and s[left] == s[right]:
10+
left -= 1
11+
right += 1
12+
return s[left + 1 : right]
13+
14+
max_str = s[0]
15+
16+
for i in range(len(s) - 1):
17+
odd = expand_from_center(i, i)
18+
even = expand_from_center(i, i + 1)
19+
20+
if len(odd) > len(max_str):
21+
max_str = odd
22+
if len(even) > len(max_str):
23+
max_str = even
24+
25+
return max_str
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# O(n+e) where n is number of nodes and e is the number of edges.
2+
# O(n+e) where n is number of nodes and e is the number of edges.
3+
class Solution:
4+
def countComponents(self, numNodes: int, connections: List[List[int]]) -> int:
5+
adjacencyList = [[] for _ in range(numNodes)]
6+
for src, dst in connections:
7+
adjacencyList[src].append(dst)
8+
adjacencyList[dst].append(src)
9+
10+
visitedNodes = set()
11+
12+
def depthFirstSearch(node):
13+
visitedNodes.add(node)
14+
for neighbor in adjacencyList[node]:
15+
if neighbor not in visitedNodes:
16+
depthFirstSearch(neighbor)
17+
18+
componentCount = 0
19+
for node in range(numNodes):
20+
if node not in visitedNodes:
21+
componentCount += 1
22+
depthFirstSearch(node)
23+
return componentCount

0 commit comments

Comments
 (0)