diff --git a/best-time-to-buy-and-sell-stock/Chaedie.py b/best-time-to-buy-and-sell-stock/Chaedie.py new file mode 100644 index 000000000..99db7f549 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/Chaedie.py @@ -0,0 +1,40 @@ +""" +Solution: 1) 2중 포문을 돌면서 max값을 구한다. +Time: O(n^2) +Space: O(1) + +Time Limit Exceeded + +""" + + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + result = 0 + for l in range(len(prices) - 1): + for r in range(l + 1, len(prices)): + result = max(result, prices[r] - prices[l]) + + return result + + +""" +Solution: + 1) prices를 순회하면서 max_profit 을 찾는다. + 2) profit 은 current price - min_profit로 구한다. +Time: O(n) +Space: O(1) +""" + + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + n = len(prices) + max_profit = 0 + min_price = prices[0] + + for i in range(1, len(prices)): + profit = prices[i] - min_price + max_profit = max(max_profit, profit) + min_price = min(prices[i], min_price) + return max_profit diff --git a/group-anagrams/Chaedie.py b/group-anagrams/Chaedie.py new file mode 100644 index 000000000..43d3e29d7 --- /dev/null +++ b/group-anagrams/Chaedie.py @@ -0,0 +1,22 @@ +""" +Solution: + 1) hash map 에 sorted_word를 키로, 해당 sorted_word 에 해당하는 요소들을 밸류로 넣습니다. +Time: O(n^2 logn)= O(n) * O(nlogn) +Space: O(n) +""" + + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + anagram_words = defaultdict(list) + # dict = {sorted_word: [word, word]} + + for i in range(len(strs)): + word = strs[i] + sorted_word = "".join(sorted(word)) + anagram_words[sorted_word].append(word) + + result = [] + for arr in anagram_words.values(): + result.append(arr) + return result diff --git a/implement-trie-prefix-tree/Chaedie.py b/implement-trie-prefix-tree/Chaedie.py new file mode 100644 index 000000000..e1de718e2 --- /dev/null +++ b/implement-trie-prefix-tree/Chaedie.py @@ -0,0 +1,59 @@ +""" +Solution: + Trie 구조에 대해 배워보았습니다. + TrieNode 는 children과 ending으로 이루어져있습니다. + 1) insert: + 1.1) 문자열의 각 문자를 Trie 구조에 넣습니다. + 1.2) 마지막 문자열에선 ending 을 True 로 set합니다. + 2) search: + 2.1) 문자열의 각 문자가 node.children 에 존재하지 않으면 False 를 반환합니다. + 2.2) 마지막 문자가 ending 인지 판별합니다. + 3) startsWith + 3.1) search 와 동일하게 문자열을 순회합니다. + 3.2) ending 여부와 무관하게 True 를 반환합니다. +""" + + +class TrieNode: + def __init__(self): + self.children = {} + self.ending = False + + +class Trie: + def __init__(self): + self.root = TrieNode() + + def insert(self, word: str) -> None: + node = self.root + + for char in word: + if char not in node.children: + node.children[char] = TrieNode() + node = node.children[char] + node.ending = True + + def search(self, word: str) -> bool: + node = self.root + + for char in word: + if char not in node.children: + return False + node = node.children[char] + return node.ending + + def startsWith(self, prefix: str) -> bool: + node = self.root + + for char in prefix: + if char not in node.children: + return False + node = node.children[char] + 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)