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..fe65fd852a --- /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_profit = 0 + for cur_price in prices: + if max_profit < cur_price - min_price: + max_profit = cur_price - min_price + + if cur_price < min_price: + min_price = cur_price + + return max_profit 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)) 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()) diff --git a/implement-trie-prefix-tree/parkhojeong.py b/implement-trie-prefix-tree/parkhojeong.py new file mode 100644 index 0000000000..4b77a963e5 --- /dev/null +++ b/implement-trie-prefix-tree/parkhojeong.py @@ -0,0 +1,68 @@ +class Node: + def __init__(self, s: str): + 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) diff --git a/word-break/parkhojeong.py b/word-break/parkhojeong.py new file mode 100644 index 0000000000..7eb9b15123 --- /dev/null +++ b/word-break/parkhojeong.py @@ -0,0 +1,25 @@ +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + word_len_set = set(len(word) for word in wordDict) + word_set = set(word for word in wordDict) + visited = set() + + def dfs(s: str): + if len(s) == 0: + return True + + if s in visited: + return False + + visited.add(s) + + for word_len in word_len_set: + if s[:word_len] not in word_set: + continue + + if dfs(s[word_len:]): + return True + + return False + + return dfs(s)