Skip to content

Evan W10 #169

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 5 commits into from
Jul 7, 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
40 changes: 40 additions & 0 deletions graph-valid-tree/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from typing import List


class Solution:
def validTree(self, n: int, edges: List[List[int]]) -> bool:
# If the number of edges is not equal to n-1, it can't be a tree
if len(edges) != n - 1:
return False

# Initialize node_list array where each node is its own parent
node_list = list(range(n))

# Find root function with path compression
def find_root(node):
if node_list[node] != node:
node_list[node] = find_root(node_list[node])

return node_list[node]

def union_and_check_cycle(node1, node2):
root1 = find_root(node1)
root2 = find_root(node2)

if root1 != root2:
node_list[root2] = root1
return True
else:
return False

for node1, node2 in edges:
if not union_and_check_cycle(node1, node2):
return False

return True


# Example usage:
solution = Solution()
print(solution.validTree(5, [[0, 1], [0, 2], [0, 3], [1, 4]])) # Output: True
print(solution.validTree(5, [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]])) # Output: False
31 changes: 31 additions & 0 deletions house-robber-ii/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import List


class Solution:
def rob(self, nums: List[int]) -> int:
def linear_rob(nums: List[int]) -> int:
house_length = len(nums)

if house_length == 0:
return 0
if house_length == 1:
return nums[0]
if house_length == 2:
return max(nums[0], nums[1])

dp = [nums[0], max(nums[0], nums[1])]

for index in range(2, house_length):
dp.append(max(dp[index - 1], dp[index - 2] + nums[index]))

return dp[-1]

house_length = len(nums)

if house_length < 3:
return linear_rob(nums)

first_house_selected_result = linear_rob(nums[:-1])
second_house_selected_result = linear_rob(nums[1:])

return max(first_house_selected_result, second_house_selected_result)
20 changes: 20 additions & 0 deletions house-robber/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import List


class Solution:
def rob(self, nums: List[int]) -> int:
house_length = len(nums)

if house_length == 0:
return 0
if house_length == 1:
return nums[0]
if house_length == 2:
return max(nums[0], nums[1])

dp = [nums[0], max(nums[0], nums[1])]

for index in range(2, house_length):
dp.append(max(dp[index - 1], dp[index - 2] + nums[index]))

return dp[-1]
25 changes: 25 additions & 0 deletions longest-palindromic-substring/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def longestPalindrome(self, s: str) -> str:
if not s:
return ""

start, end = 0, 0

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

return right - left - 1

for i in range(len(s)):
odd_palindrome_length = expand_and_get_length(s, i, i)
even_palindrome_length = expand_and_get_length(s, i, i + 1)

max_len = max(odd_palindrome_length, even_palindrome_length)

if max_len > end - start:
start = i - (max_len - 1) // 2
end = i + max_len // 2

return s[start : end + 1]
47 changes: 47 additions & 0 deletions number-of-connected-components-in-an-undirected-graph/evan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from typing import List


class Solution:
"""
@param n: the number of vertices
@param edges: the edges of undirected graph
@return: the number of connected components
"""

def count_components(self, n: int, edges: List[List[int]]) -> int:
# Initialize each node to be its own parent
vertex_list = list(range(n))

# Helper function to find the root of a node
def find(node):
while vertex_list[node] != node:
node = vertex_list[node]
return node

# Iterate through each edge and perform union
for edge in edges:
root1 = find(edge[0])
root2 = find(edge[1])

# If roots are different, union the components
if root1 != root2:
for i in range(n):
if vertex_list[i] == root1:
vertex_list[i] = root2

# Find all unique roots to count the number of connected components
unique_roots = set(find(i) for i in range(n))
return len(unique_roots)


# Test Cases
solution = Solution()

print(solution.count_components(4, [[0, 1], [2, 3], [3, 1]])) # Output: 1
print(solution.count_components(5, [[0, 1], [1, 2], [3, 4]])) # Output: 2
print(solution.count_components(5, [[0, 1], [1, 2], [2, 3], [3, 4]])) # Output: 1
print(solution.count_components(3, [[0, 1], [1, 2], [2, 0]])) # Output: 1
print(solution.count_components(4, [])) # Output: 4
print(
solution.count_components(7, [[0, 1], [1, 2], [3, 4], [4, 5], [5, 6]])
) # Output: 2