Skip to content

Commit 4452b1c

Browse files
authored
Merge pull request DaleStudy#141 from SamTheKorean/solution8
[SAM] Week 8 solutions
2 parents 9452e8f + 893fd6d commit 4452b1c

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

combination-sum/samthekorean.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# TC : O(n^2)
2+
# SC : O(n)
3+
class Solution:
4+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
5+
result = []
6+
7+
def dfs(start: int, total: int, current_combination: List[int]):
8+
if total > target:
9+
return
10+
if total == target:
11+
result.append(current_combination[:])
12+
return
13+
for i in range(start, len(candidates)):
14+
current_combination.append(candidates[i])
15+
dfs(i, total + candidates[i], current_combination)
16+
current_combination.pop()
17+
18+
dfs(0, 0, [])
19+
return result
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# TC : O(n)
2+
# SC : O(n)
3+
class Solution:
4+
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
5+
if not preorder or not inorder:
6+
return None
7+
8+
root = TreeNode(preorder[0])
9+
mid = inorder.index(preorder[0])
10+
root.left = self.buildTree(preorder[1 : mid + 1], inorder[:mid])
11+
root.right = self.buildTree(preorder[mid + 1 :], inorder[mid + 1 :])
12+
return root

implement-trie-prefix-tree/samthekorean.py

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# TC : O(n)
2+
# SC : O(n)
3+
class Solution:
4+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
5+
n = 0
6+
stack = []
7+
cur = root
8+
9+
while cur or stack:
10+
while cur:
11+
stack.append(cur)
12+
cur = cur.left
13+
14+
cur = stack.pop()
15+
n += 1
16+
if n == k:
17+
return cur.val
18+
19+
cur = cur.right
20+
21+
return -1

word-search/samthekorean.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# TC : O(N * 4^L), where N is the number of cells in the board and L is the length of the word.
2+
# SC : O(L) due to the recursive calls in the backtrack function, where L is the length of the word.
3+
class Solution:
4+
def exist(self, board, word):
5+
def backtrack(i, j, k):
6+
# If we have checked all characters in the word
7+
if k == len(word):
8+
return True
9+
# If out of bounds or current cell does not match the word character
10+
if (
11+
i < 0
12+
or i >= len(board)
13+
or j < 0
14+
or j >= len(board[0])
15+
or board[i][j] != word[k]
16+
):
17+
return False
18+
19+
# Temporarily mark the cell as visited
20+
temp = board[i][j]
21+
board[i][j] = ""
22+
23+
# Explore all possible directions: down, up, right, left
24+
found = (
25+
backtrack(i + 1, j, k + 1)
26+
or backtrack(i - 1, j, k + 1)
27+
or backtrack(i, j + 1, k + 1)
28+
or backtrack(i, j - 1, k + 1)
29+
)
30+
31+
# Restore the original value of the cell
32+
board[i][j] = temp
33+
return found
34+
35+
# Start from each cell in the board
36+
for i in range(len(board)):
37+
for j in range(len(board[0])):
38+
if backtrack(i, j, 0):
39+
return True
40+
return False

0 commit comments

Comments
 (0)