Skip to content

[SAM] Week 10 solutions #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 8, 2024
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
25 changes: 25 additions & 0 deletions graph-valid-tree/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# O(n+e) where n is number of nodes and e is the number of edges.
# O(n+e) where n is number of nodes and e is the number of edges.
class Solution:
def validTree(self, numNodes: int, connections: List[List[int]]) -> bool:
adjacencyList = [[] for _ in range(numNodes)]
for src, dst in connections:
adjacencyList[src].append(dst)
adjacencyList[dst].append(src)

visitedNodes = set()

def detectCycle(currentNode, previousNode):
if currentNode in visitedNodes:
return True
visitedNodes.add(currentNode)
for neighbor in adjacencyList[currentNode]:
if neighbor == previousNode:
continue
if detectCycle(neighbor, currentNode):
return True
return False

if detectCycle(0, -1):
return False
return len(visitedNodes) == numNodes
30 changes: 30 additions & 0 deletions house-robber-ii/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# TC : O(n), where n is the number of houses.
# SC : O(1)
class Solution:
def rob(self, nums):
n = len(nums)
if n == 1:
return nums[0]
if n == 2:
return max(nums[0], nums[1])
if n == 3:
return max(nums[0], max(nums[1], nums[2]))
Comment on lines +6 to +11
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 n <= 3일 때 max(nums)로 해결할 수 있을 것 같아보여요. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇게 하면 더 간결하겠군요! 피드백 감사합니다!


a = nums[0]
b = max(nums[0], nums[1])
c = -1
a1 = nums[1]
b1 = max(nums[1], nums[2])
c1 = -1

for i in range(2, n):
if i < n - 1:
c = max(nums[i] + a, b)
a = b
b = c
if i > 2:
c1 = max(nums[i] + a1, b1)
a1 = b1
b1 = c1

return max(c, c1)
19 changes: 19 additions & 0 deletions house-robber/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# TC : O(n)
# SC : O(n)
class Solution:
def rob(self, nums):
n = len(nums)
if n == 0:
return 0
if n == 1:
return nums[0]

dp = [-1] * n
dp[0] = nums[0]
if n > 1:
dp[1] = max(nums[0], nums[1])

for i in range(2, n):
dp[i] = max(dp[i - 1], nums[i] + dp[i - 2])

return dp[-1]
25 changes: 25 additions & 0 deletions longest-palindromic-substring/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# O(n^2)
# O(1)
class Solution:
def longestPalindrome(self, s: str) -> str:
if len(s) <= 1:
return s

def expand_from_center(left, right):
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return s[left + 1 : right]

max_str = s[0]

for i in range(len(s) - 1):
odd = expand_from_center(i, i)
even = expand_from_center(i, i + 1)

if len(odd) > len(max_str):
max_str = odd
if len(even) > len(max_str):
max_str = even

return max_str
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# O(n+e) where n is number of nodes and e is the number of edges.
# O(n+e) where n is number of nodes and e is the number of edges.
class Solution:
def countComponents(self, numNodes: int, connections: List[List[int]]) -> int:
adjacencyList = [[] for _ in range(numNodes)]
for src, dst in connections:
adjacencyList[src].append(dst)
adjacencyList[dst].append(src)

visitedNodes = set()

def depthFirstSearch(node):
visitedNodes.add(node)
for neighbor in adjacencyList[node]:
if neighbor not in visitedNodes:
depthFirstSearch(neighbor)

componentCount = 0
for node in range(numNodes):
if node not in visitedNodes:
componentCount += 1
depthFirstSearch(node)
return componentCount