diff --git a/combination-sum/Leo.py b/combination-sum/Leo.py new file mode 100644 index 000000000..e345049cb --- /dev/null +++ b/combination-sum/Leo.py @@ -0,0 +1,14 @@ +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + dp = [[] for i in range(target + 1)] + dp[0] = [[]] + + for num in candidates: + for i in range(num, target + 1): + for comb in dp[i - num]: + dp[i].append(comb + [num]) + + return dp[target] + + ## TC: O(outer loop * 1st inner(target size) * 2nd inner(combination #)) + ## SC: O(target size * combination #) diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/Leo.py b/construct-binary-tree-from-preorder-and-inorder-traversal/Leo.py new file mode 100644 index 000000000..b0aa8b303 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/Leo.py @@ -0,0 +1,18 @@ +class Solution: + def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: + if len(inorder) == 0: + return None + + if len(preorder) == 1: + return TreeNode(preorder[0]) + + idx = inorder.index(preorder.pop(0)) + node = TreeNode(inorder[idx]) + + node.left = self.buildTree(preorder, inorder[:idx]) + node.right = self.buildTree(preorder, inorder[idx + 1:]) + + return node + + + ## TC: O(n^2), SC: O(n) diff --git a/implement-trie-prefix-tree/Leo.py b/implement-trie-prefix-tree/Leo.py new file mode 100644 index 000000000..1e24f9fcb --- /dev/null +++ b/implement-trie-prefix-tree/Leo.py @@ -0,0 +1,42 @@ +class TrieNode: + def __init__(self): + self.children = {} + self.isEnd = False + + +class Trie: + def __init__(self): + self.root = TrieNode() + + def insert(self, word: str) -> None: + cur = self.root + + for c in word: + if c not in cur.children: + cur.children[c] = TrieNode() + cur = cur.children[c] + cur.isEnd = True + + ## TC: O(len(word)), SC: O(len(word)) + + def search(self, word: str) -> bool: + cur = self.root + + for c in word: + if c not in cur.children: + return False + cur = cur.children[c] + return cur.isEnd + + ## TC: O(len(word)), SC: O(1) + + def startsWith(self, prefix: str) -> bool: + cur = self.root + + for c in prefix: + if c not in cur.children: + return False + cur = cur.children[c] + return True + + ## TC: O(len(prefix)). SC: O(1) diff --git a/kth-smallest-element-in-a-bst/Leo.py b/kth-smallest-element-in-a-bst/Leo.py new file mode 100644 index 000000000..bcdedf02b --- /dev/null +++ b/kth-smallest-element-in-a-bst/Leo.py @@ -0,0 +1,18 @@ +class Solution: + def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: + ans = [] + + def helper(node): + if not node: return + helper(node.left) + + if len(ans) == k: + return + + ans.append(node.val) + helper(node.right) + + helper(root) + return ans[-1] + + ## TC: O(n), SC: O(h+n) where h == heights(tree) and n == element(tree) diff --git a/word-search/Leo.py b/word-search/Leo.py new file mode 100644 index 000000000..0f179c900 --- /dev/null +++ b/word-search/Leo.py @@ -0,0 +1,30 @@ +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + rows, cols = len(board), len(board[0]) + + def backtrack(r, c, index): + if index == len(word): + return True + if r < 0 or r >= rows or c < 0 or c >= cols or board[r][c] != word[index]: + return False + + temp = board[r][c] + board[r][c] = "!" + + found = (backtrack(r + 1, c, index + 1) or + backtrack(r - 1, c, index + 1) or + backtrack(r, c + 1, index + 1) or + backtrack(r, c - 1, index + 1)) + + board[r][c] = temp + return found + + for i in range(rows): + for j in range(cols): + if backtrack(i, j, 0): + return True + + return False + + ## TC: O(D of row * D of Cols * 4^L), but amortised could be O(DoR * DoC) + ## SC: O(L)