Skip to content

Commit 34c9f96

Browse files
committed
Add solution of combination-sum and formatting
1 parent 44d145f commit 34c9f96

File tree

6 files changed

+76
-23
lines changed

6 files changed

+76
-23
lines changed

combination-sum/bhyun-kim.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
39. Combination Sum
3+
https://leetcode.com/problems/combination-sum/
4+
5+
Solution
6+
To solve this problem, we can use backtracking.
7+
The idea is to explore all possible combinations starting from each candidate.
8+
9+
- We can sort the candidates to avoid duplicates.
10+
- We can create a helper function that takes the remaining target, the current combination, and the start index.
11+
- If the remaining target is 0, we have found a valid combination, so we add it to the result.
12+
- If the remaining target is negative, we return.
13+
- We iterate through the candidates starting from the start index.
14+
- We add the current candidate to the combination and recursively call the helper function with the updated target and combination.
15+
- After the recursive call, we remove the current candidate from the combination.
16+
17+
Time complexity: O(2^n)
18+
- In the worst case, we explore all possible combinations.
19+
- For each candidate, we have two choices: include it or exclude it.
20+
21+
Space complexity: O(n)
22+
- The recursive call stack has a maximum depth of n.
23+
"""
24+
25+
from typing import List
26+
27+
28+
class Solution:
29+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
30+
def backtrack(remain, comb, start):
31+
if remain == 0:
32+
result.append(list(comb))
33+
return
34+
elif remain < 0:
35+
return
36+
37+
for i in range(start, len(candidates)):
38+
current_candidate = candidates[i]
39+
if current_candidate > remain:
40+
break
41+
42+
comb.append(current_candidate)
43+
backtrack(remain - current_candidate, comb, i)
44+
comb.pop()
45+
46+
candidates.sort()
47+
result = []
48+
backtrack(target, [], 0)
49+
return result

construct-binary-tree-from-preorder-and-inorder-traversal/bhyun-kim.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55

66
from typing import List, Optional
77

8+
89
# Definition for a binary tree node.
910
class TreeNode:
1011
def __init__(self, val=0, left=None, right=None):
1112
self.val = val
1213
self.left = left
1314
self.right = right
1415

16+
1517
"""
1618
Solution
1719
To solve this problem, we can use a recursive approach.
@@ -37,6 +39,7 @@ def __init__(self, val=0, left=None, right=None):
3739
- The recursive call stack has n elements.
3840
"""
3941

42+
4043
class Solution:
4144
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
4245
inorder_index_map = {val: idx for idx, val in enumerate(inorder)}
@@ -57,4 +60,4 @@ def array_to_tree(left, right):
5760

5861
return root
5962

60-
return array_to_tree(0, len(inorder) - 1)
63+
return array_to_tree(0, len(inorder) - 1)

implement-trie-prefix-tree/bhyun-kim.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
- The list of words may have all the words in the trie.
2121
"""
2222

23-
class Trie:
2423

24+
class Trie:
2525
def __init__(self):
2626
self.words = []
2727

2828
def insert(self, word: str) -> None:
2929
self.words.append(word)
30-
30+
3131
def search(self, word: str) -> bool:
3232
return word in self.words
3333

@@ -36,12 +36,11 @@ def startsWith(self, prefix: str) -> bool:
3636
if word.startswith(prefix):
3737
return True
3838

39-
return False
40-
39+
return False
4140

4241

4342
# Your Trie object will be instantiated and called as such:
4443
# obj = Trie()
4544
# obj.insert(word)
4645
# param_2 = obj.search(word)
47-
# param_3 = obj.startsWith(prefix)
46+
# param_3 = obj.startsWith(prefix)

kth-smallest-element-in-a-bst/bhyun-kim.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@
1818

1919
from typing import Optional
2020

21+
2122
# Definition for a binary tree node.
2223
class TreeNode:
2324
def __init__(self, val=0, left=None, right=None):
2425
self.val = val
2526
self.left = left
2627
self.right = right
2728

29+
2830
class Solution:
2931
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
30-
return self.inOrderSearch(root)[k-1]
32+
return self.inOrderSearch(root)[k - 1]
3133

3234
def inOrderSearch(self, root):
3335
output = []
34-
if root:
36+
if root:
3537
output += self.inOrderSearch(root.left)
3638
output += [root.val]
3739
output += self.inOrderSearch(root.right)
38-
return output
40+
return output

validate-binary-search-tree/bhyun-kim.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,12 @@ def isValidBST(self, root: Optional[TreeNode]) -> bool:
4343
return self.isValidSubTree(root, maximum, minimum)
4444

4545
def isValidSubTree(self, root, maximum, minimum):
46-
4746
if root is None:
4847
return True
4948

50-
if not minimum < root.val < maximum:
51-
return False
52-
49+
if not minimum < root.val < maximum:
50+
return False
51+
5352
return self.isValidSubTree(
5453
root.left, root.val, minimum
55-
) and self.isValidSubTree(
56-
root.right, maximum, root.val
57-
)
54+
) and self.isValidSubTree(root.right, maximum, root.val)

word-search/bhyun-kim.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@
3737

3838
from typing import List
3939

40+
4041
class Solution:
4142
def exist(self, board: List[List[str]], word: str) -> bool:
4243
if not board or not word:
4344
return False
4445

4546
m, n = len(board), len(board[0])
46-
47+
4748
# Step 1: Check if all characters in the word exist in the board
4849
char_count = {}
4950
for row in board:
@@ -62,18 +63,20 @@ def exist(self, board: List[List[str]], word: str) -> bool:
6263
def dfs(i, j, word_index):
6364
if word_index == len(word):
6465
return True
65-
66+
6667
if i < 0 or i >= m or j < 0 or j >= n or board[i][j] != word[word_index]:
6768
return False
6869

6970
temp = board[i][j]
7071
board[i][j] = "#" # mark as visited
7172

7273
# Explore all possible directions
73-
found = (dfs(i + 1, j, word_index + 1) or
74-
dfs(i - 1, j, word_index + 1) or
75-
dfs(i, j + 1, word_index + 1) or
76-
dfs(i, j - 1, word_index + 1))
74+
found = (
75+
dfs(i + 1, j, word_index + 1)
76+
or dfs(i - 1, j, word_index + 1)
77+
or dfs(i, j + 1, word_index + 1)
78+
or dfs(i, j - 1, word_index + 1)
79+
)
7780

7881
board[i][j] = temp # unmark
7982
return found
@@ -84,4 +87,4 @@ def dfs(i, j, word_index):
8487
if board[i][j] == word[0] and dfs(i, j, 0):
8588
return True
8689

87-
return False
90+
return False

0 commit comments

Comments
 (0)