From 0630c80eafbf9db08f22aff504099b6fb6bded64 Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Thu, 23 Jul 2026 01:30:28 +0900 Subject: [PATCH 1/8] best time to buy and sell stock solution --- best-time-to-buy-and-sell-stock/parkhojeong.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/parkhojeong.py diff --git a/best-time-to-buy-and-sell-stock/parkhojeong.py b/best-time-to-buy-and-sell-stock/parkhojeong.py new file mode 100644 index 0000000000..9f376dc4a4 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/parkhojeong.py @@ -0,0 +1,12 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + min_price = 10_000 + max_price = 0 + for cur_price in prices: + if max_price < cur_price - min_price: + max_price = cur_price - min_price + + if cur_price < min_price: + min_price = cur_price + + return max_price From 6328dc6ab84d62b08d2924d4454af2470c0e7f66 Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Thu, 23 Jul 2026 01:30:29 +0900 Subject: [PATCH 2/8] group anagrams solution --- group-anagrams/parkhojeong.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 group-anagrams/parkhojeong.py diff --git a/group-anagrams/parkhojeong.py b/group-anagrams/parkhojeong.py new file mode 100644 index 0000000000..28877a8cae --- /dev/null +++ b/group-anagrams/parkhojeong.py @@ -0,0 +1,8 @@ +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + anagram_dic = {} + for s in strs: + key = "".join(sorted(s)) + anagram_dic.setdefault(key, []).append(s) + + return list(anagram_dic.values()) From 905eb330737fc7fda858a369f008e60b9dc89693 Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Thu, 23 Jul 2026 01:30:29 +0900 Subject: [PATCH 3/8] encode and decode strings solution --- encode-and-decode-strings/parkhojeong.py | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 encode-and-decode-strings/parkhojeong.py diff --git a/encode-and-decode-strings/parkhojeong.py b/encode-and-decode-strings/parkhojeong.py new file mode 100644 index 0000000000..1bebb1d2f5 --- /dev/null +++ b/encode-and-decode-strings/parkhojeong.py @@ -0,0 +1,28 @@ +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string. + """ + encoded = "" + for s in strs: + encoded += f"{len(s):03d}{s}" + return encoded + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings. + """ + + decoded = [] + i = 0 + while i < len(s): + s_start_idx = i + 3 + s_len = int(s[i:s_start_idx]) + decoded.append(s[s_start_idx:s_start_idx + s_len]) + + i = s_start_idx + s_len + + return decoded + + +# Your Codec object will be instantiated and called as such: +# codec = Codec() +# codec.decode(codec.encode(strs)) From 6dd411d8a947f8b8d38221d6bac1ad5fce0e5805 Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Thu, 23 Jul 2026 01:30:29 +0900 Subject: [PATCH 4/8] implement trie prefix tree solution --- implement-trie-prefix-tree/parkhojeong.py | 69 +++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 implement-trie-prefix-tree/parkhojeong.py diff --git a/implement-trie-prefix-tree/parkhojeong.py b/implement-trie-prefix-tree/parkhojeong.py new file mode 100644 index 0000000000..c2ffb411b9 --- /dev/null +++ b/implement-trie-prefix-tree/parkhojeong.py @@ -0,0 +1,69 @@ +class Node: + def __init__(self, s: str): + self.s: str = s + self.isLast: bool = False + self.childs: dict[str, Node] = {} + + def addChild(self, s: str): + if self.getChild(s) is not None: + return + + node = Node(s) + self.childs[s] = node + + def getChild(self, s: str) -> Node | None: + if s not in self.childs: + return None + + return self.childs[s] + + def setIsLast(self, isLast: bool): + self.isLast = isLast + + def getIsLast(self) -> bool: + return self.isLast + + +class Trie: + root: Node + + def __init__(self): + self.root = Node("") + + def insert(self, word: str) -> None: + node = self.root + for i in range(len(word)): + ch = word[i] + if node.getChild(ch) is None: + node.addChild(ch) + + node = node.getChild(ch) + if i == len(word) - 1: + node.setIsLast(True) + + def findNode(self, word) -> Node | None: + node = self.root + for ch in word: + node = node.getChild(ch) + if node is None: + return None + + return node + + def search(self, word: str) -> bool: + node = self.findNode(word) + if node is None or not node.getIsLast(): + return False + + return True + + def startsWith(self, prefix: str) -> bool: + return self.findNode(prefix) is not None + + + +# Your Trie object will be instantiated and called as such: +# obj = Trie() +# obj.insert(word) +# param_2 = obj.search(word) +# param_3 = obj.startsWith(prefix) From f143fb48b89245a48a563d509e2f19eb7cf2acb3 Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Thu, 23 Jul 2026 01:30:29 +0900 Subject: [PATCH 5/8] word break solution --- word-break/parkhojeong.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 word-break/parkhojeong.py diff --git a/word-break/parkhojeong.py b/word-break/parkhojeong.py new file mode 100644 index 0000000000..05e39d8fc8 --- /dev/null +++ b/word-break/parkhojeong.py @@ -0,0 +1,38 @@ +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + first_letter_dict = {} + for word in wordDict: + first_letter_dict.setdefault(word[0], []).append(word) + + last_letter_dict = {} + for word in wordDict: + last_letter_dict.setdefault(word[-1], []).append(word) + + for word in wordDict: + isSuccess = True + for small_s in s.split(word): + if small_s == "": + continue + if not self.dfs(small_s, 0, first_letter_dict, last_letter_dict): + isSuccess = False + + if isSuccess: + return True + + return False + + def dfs(self, s: str, i, first_letter_dict, last_letter_dict): + if i == len(s): + return True + + if s[i] not in first_letter_dict or s[-1] not in last_letter_dict: + return False + + for word in first_letter_dict[s[i]]: + if not s[i:].startswith(word): + continue + + if self.dfs(s, i + len(word), first_letter_dict, last_letter_dict): + return True + + return False From 78d8ad559ad1f3f8c616ed8feaf88737f854ba3b Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Thu, 23 Jul 2026 12:04:15 +0900 Subject: [PATCH 6/8] improve best-time-to-buy-and-sell-stock solution --- best-time-to-buy-and-sell-stock/parkhojeong.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/best-time-to-buy-and-sell-stock/parkhojeong.py b/best-time-to-buy-and-sell-stock/parkhojeong.py index 9f376dc4a4..fe65fd852a 100644 --- a/best-time-to-buy-and-sell-stock/parkhojeong.py +++ b/best-time-to-buy-and-sell-stock/parkhojeong.py @@ -1,12 +1,12 @@ class Solution: def maxProfit(self, prices: List[int]) -> int: min_price = 10_000 - max_price = 0 + max_profit = 0 for cur_price in prices: - if max_price < cur_price - min_price: - max_price = cur_price - min_price + if max_profit < cur_price - min_price: + max_profit = cur_price - min_price if cur_price < min_price: min_price = cur_price - return max_price + return max_profit From 8facb2ecafa6f7580e260f37308a2e5c95d4b332 Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Thu, 23 Jul 2026 12:04:15 +0900 Subject: [PATCH 7/8] improve word-break solution --- word-break/parkhojeong.py | 43 ++++++++++++++------------------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/word-break/parkhojeong.py b/word-break/parkhojeong.py index 05e39d8fc8..7eb9b15123 100644 --- a/word-break/parkhojeong.py +++ b/word-break/parkhojeong.py @@ -1,38 +1,25 @@ class Solution: def wordBreak(self, s: str, wordDict: List[str]) -> bool: - first_letter_dict = {} - for word in wordDict: - first_letter_dict.setdefault(word[0], []).append(word) + word_len_set = set(len(word) for word in wordDict) + word_set = set(word for word in wordDict) + visited = set() - last_letter_dict = {} - for word in wordDict: - last_letter_dict.setdefault(word[-1], []).append(word) - - for word in wordDict: - isSuccess = True - for small_s in s.split(word): - if small_s == "": - continue - if not self.dfs(small_s, 0, first_letter_dict, last_letter_dict): - isSuccess = False - - if isSuccess: + def dfs(s: str): + if len(s) == 0: return True - return False + if s in visited: + return False - def dfs(self, s: str, i, first_letter_dict, last_letter_dict): - if i == len(s): - return True + visited.add(s) - if s[i] not in first_letter_dict or s[-1] not in last_letter_dict: - return False + for word_len in word_len_set: + if s[:word_len] not in word_set: + continue - for word in first_letter_dict[s[i]]: - if not s[i:].startswith(word): - continue + if dfs(s[word_len:]): + return True - if self.dfs(s, i + len(word), first_letter_dict, last_letter_dict): - return True + return False - return False + return dfs(s) From 50caae08077b5cb2ab8206d18beeb89e533017dc Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Thu, 23 Jul 2026 12:10:22 +0900 Subject: [PATCH 8/8] improve implement-trie-prefix-tree solution --- implement-trie-prefix-tree/parkhojeong.py | 1 - 1 file changed, 1 deletion(-) diff --git a/implement-trie-prefix-tree/parkhojeong.py b/implement-trie-prefix-tree/parkhojeong.py index c2ffb411b9..4b77a963e5 100644 --- a/implement-trie-prefix-tree/parkhojeong.py +++ b/implement-trie-prefix-tree/parkhojeong.py @@ -1,6 +1,5 @@ class Node: def __init__(self, s: str): - self.s: str = s self.isLast: bool = False self.childs: dict[str, Node] = {}