diff --git a/graph-valid-tree/samthekorean.py b/graph-valid-tree/samthekorean.py new file mode 100644 index 000000000..05f701146 --- /dev/null +++ b/graph-valid-tree/samthekorean.py @@ -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 diff --git a/house-robber-ii/samthekorean.py b/house-robber-ii/samthekorean.py new file mode 100644 index 000000000..a9ba8eb09 --- /dev/null +++ b/house-robber-ii/samthekorean.py @@ -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])) + + 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) diff --git a/house-robber/samthekorean.py b/house-robber/samthekorean.py new file mode 100644 index 000000000..33ce191d3 --- /dev/null +++ b/house-robber/samthekorean.py @@ -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] diff --git a/longest-palindromic-substring/samthekorean.py b/longest-palindromic-substring/samthekorean.py new file mode 100644 index 000000000..ce9800a50 --- /dev/null +++ b/longest-palindromic-substring/samthekorean.py @@ -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 diff --git a/number-of-connected-components-in-an-undirected-graph/samthekorean.py b/number-of-connected-components-in-an-undirected-graph/samthekorean.py new file mode 100644 index 000000000..5cd746629 --- /dev/null +++ b/number-of-connected-components-in-an-undirected-graph/samthekorean.py @@ -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