Skip to content

Commit f2970ad

Browse files
authored
Merge pull request #142 from leokim0922/main
[Leo] 8th Week solutions
2 parents 4452b1c + cc52aba commit f2970ad

File tree

5 files changed

+122
-0
lines changed
  • combination-sum
  • construct-binary-tree-from-preorder-and-inorder-traversal
  • implement-trie-prefix-tree
  • kth-smallest-element-in-a-bst

5 files changed

+122
-0
lines changed

combination-sum/Leo.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
3+
dp = [[] for i in range(target + 1)]
4+
dp[0] = [[]]
5+
6+
for num in candidates:
7+
for i in range(num, target + 1):
8+
for comb in dp[i - num]:
9+
dp[i].append(comb + [num])
10+
11+
return dp[target]
12+
13+
## TC: O(outer loop * 1st inner(target size) * 2nd inner(combination #))
14+
## SC: O(target size * combination #)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
3+
if len(inorder) == 0:
4+
return None
5+
6+
if len(preorder) == 1:
7+
return TreeNode(preorder[0])
8+
9+
idx = inorder.index(preorder.pop(0))
10+
node = TreeNode(inorder[idx])
11+
12+
node.left = self.buildTree(preorder, inorder[:idx])
13+
node.right = self.buildTree(preorder, inorder[idx + 1:])
14+
15+
return node
16+
17+
18+
## TC: O(n^2), SC: O(n)

implement-trie-prefix-tree/Leo.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.children = {}
4+
self.isEnd = False
5+
6+
7+
class Trie:
8+
def __init__(self):
9+
self.root = TrieNode()
10+
11+
def insert(self, word: str) -> None:
12+
cur = self.root
13+
14+
for c in word:
15+
if c not in cur.children:
16+
cur.children[c] = TrieNode()
17+
cur = cur.children[c]
18+
cur.isEnd = True
19+
20+
## TC: O(len(word)), SC: O(len(word))
21+
22+
def search(self, word: str) -> bool:
23+
cur = self.root
24+
25+
for c in word:
26+
if c not in cur.children:
27+
return False
28+
cur = cur.children[c]
29+
return cur.isEnd
30+
31+
## TC: O(len(word)), SC: O(1)
32+
33+
def startsWith(self, prefix: str) -> bool:
34+
cur = self.root
35+
36+
for c in prefix:
37+
if c not in cur.children:
38+
return False
39+
cur = cur.children[c]
40+
return True
41+
42+
## TC: O(len(prefix)). SC: O(1)

kth-smallest-element-in-a-bst/Leo.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
3+
ans = []
4+
5+
def helper(node):
6+
if not node: return
7+
helper(node.left)
8+
9+
if len(ans) == k:
10+
return
11+
12+
ans.append(node.val)
13+
helper(node.right)
14+
15+
helper(root)
16+
return ans[-1]
17+
18+
## TC: O(n), SC: O(h+n) where h == heights(tree) and n == element(tree)

word-search/Leo.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution:
2+
def exist(self, board: List[List[str]], word: str) -> bool:
3+
rows, cols = len(board), len(board[0])
4+
5+
def backtrack(r, c, index):
6+
if index == len(word):
7+
return True
8+
if r < 0 or r >= rows or c < 0 or c >= cols or board[r][c] != word[index]:
9+
return False
10+
11+
temp = board[r][c]
12+
board[r][c] = "!"
13+
14+
found = (backtrack(r + 1, c, index + 1) or
15+
backtrack(r - 1, c, index + 1) or
16+
backtrack(r, c + 1, index + 1) or
17+
backtrack(r, c - 1, index + 1))
18+
19+
board[r][c] = temp
20+
return found
21+
22+
for i in range(rows):
23+
for j in range(cols):
24+
if backtrack(i, j, 0):
25+
return True
26+
27+
return False
28+
29+
## TC: O(D of row * D of Cols * 4^L), but amortised could be O(DoR * DoC)
30+
## SC: O(L)

0 commit comments

Comments
 (0)