Skip to content

[Leo] 8th Week solutions #142

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 3 commits into from
Jun 23, 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
14 changes: 14 additions & 0 deletions combination-sum/Leo.py
Original file line number Diff line number Diff line change
@@ -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 #)
18 changes: 18 additions & 0 deletions construct-binary-tree-from-preorder-and-inorder-traversal/Leo.py
Original file line number Diff line number Diff line change
@@ -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)
42 changes: 42 additions & 0 deletions implement-trie-prefix-tree/Leo.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions kth-smallest-element-in-a-bst/Leo.py
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 30 additions & 0 deletions word-search/Leo.py
Original file line number Diff line number Diff line change
@@ -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)