From 6d1146abb4bf5f447c333a63cf4036a85fd8bf07 Mon Sep 17 00:00:00 2001 From: YOUNGJIN NA <120540450+ppxyn1@users.noreply.github.com> Date: Fri, 12 Dec 2025 19:07:10 +0900 Subject: [PATCH] [:solved] Week5 --- best-time-to-buy-and-sell-stock/ppxyn1.py | 12 ++++++ group-anagrams/ppxyn1.py | 13 +++++++ implement-trie-prefix-tree/ppxyn1.py | 45 +++++++++++++++++++++++ word-break/ppxyn1.py | 18 +++++++++ 4 files changed, 88 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/ppxyn1.py create mode 100644 group-anagrams/ppxyn1.py create mode 100644 implement-trie-prefix-tree/ppxyn1.py create mode 100644 word-break/ppxyn1.py diff --git a/best-time-to-buy-and-sell-stock/ppxyn1.py b/best-time-to-buy-and-sell-stock/ppxyn1.py new file mode 100644 index 0000000000..a6508d5823 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/ppxyn1.py @@ -0,0 +1,12 @@ +# idea: - +class Solution: + def maxProfit(self, prices: List[int]) -> int: + max_profit = 0 + min_price = prices[0] + for price in prices: + max_profit = max(price - min_price, max_profit) + min_price = min(price, min_price) + return max_profit + + + diff --git a/group-anagrams/ppxyn1.py b/group-anagrams/ppxyn1.py new file mode 100644 index 0000000000..24c9477e77 --- /dev/null +++ b/group-anagrams/ppxyn1.py @@ -0,0 +1,13 @@ +# idea: sorting and dictionary + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + anagrams = {} + for word in strs: + sorted_word = str(sorted(word)) #sorted returns list + if sorted_word not in anagrams: + anagrams[sorted_word] = [] + anagrams[sorted_word].append(word) + return list(anagrams.values()) + + diff --git a/implement-trie-prefix-tree/ppxyn1.py b/implement-trie-prefix-tree/ppxyn1.py new file mode 100644 index 0000000000..ebc436eaa7 --- /dev/null +++ b/implement-trie-prefix-tree/ppxyn1.py @@ -0,0 +1,45 @@ +# idea : - + +class Node: + def __init__(self, ending=False): + self.children = {} + self.ending = ending + +class Trie: + def __init__(self): + self.root = Node(ending= True) + + def insert(self, word: str) -> None: + node = self.root + for ch in word: + if ch not in node.children: + node.children[ch] = Node() + node = node.children[ch] + node.ending = True + + def search(self, word: str) -> bool: + node = self.root + for ch in word: + if ch not in node.children: + return False + node = node.children[ch] + return node.ending + + + def startsWith(self, prefix: str) -> bool: + node = self.root + for ch in prefix: + if ch not in node.children: + return False + node = node.children[ch] + return True + + + +# 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/ppxyn1.py b/word-break/ppxyn1.py new file mode 100644 index 0000000000..b69d6f9926 --- /dev/null +++ b/word-break/ppxyn1.py @@ -0,0 +1,18 @@ +# idea: recursive +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + # TLE > (solution : memorization with @cache decoration) + # @cache : If a function is called more than once with the same arguments, it uses stored memoization results instead of recomputing. + def dfs(start): + if start == len(s): + return True + for word in wordDict: + if s[start:start+len(word)] == word: + if dfs(start+len(word)): + return True + return False + return dfs(0) + + + +