From ae60758078fbf96b6103aad59173b379e46228bf Mon Sep 17 00:00:00 2001 From: kimseongeun Date: Mon, 28 Apr 2025 15:11:57 +0900 Subject: [PATCH 1/3] week 5 solution --- best-time-to-buy-and-sell-stock/i-mprovising.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/i-mprovising.py diff --git a/best-time-to-buy-and-sell-stock/i-mprovising.py b/best-time-to-buy-and-sell-stock/i-mprovising.py new file mode 100644 index 000000000..7c24c5036 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/i-mprovising.py @@ -0,0 +1,16 @@ +""" +Time complexity O(n) +Space complexity O(1) + +Dynamic programming +""" + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + max_profit = 0 + min_price = prices[0] + for p in prices: + max_profit = max(max_profit, p - min_price) + min_price = min(min_price, p) + + return max_profit \ No newline at end of file From 86d583192e3f38075ced006efa17f08c38c39fad Mon Sep 17 00:00:00 2001 From: kimseongeun Date: Tue, 29 Apr 2025 15:10:34 +0900 Subject: [PATCH 2/3] week 5 solutions --- .../i-mprovising.py | 2 +- group-anagrams/i-mprovising.py | 15 +++++++ implement-trie-prefix-tree/i-mprovising.py | 39 +++++++++++++++++++ word-break/i-mprovising.py | 27 +++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 group-anagrams/i-mprovising.py create mode 100644 implement-trie-prefix-tree/i-mprovising.py create mode 100644 word-break/i-mprovising.py diff --git a/best-time-to-buy-and-sell-stock/i-mprovising.py b/best-time-to-buy-and-sell-stock/i-mprovising.py index 7c24c5036..9085c0207 100644 --- a/best-time-to-buy-and-sell-stock/i-mprovising.py +++ b/best-time-to-buy-and-sell-stock/i-mprovising.py @@ -13,4 +13,4 @@ def maxProfit(self, prices: List[int]) -> int: max_profit = max(max_profit, p - min_price) min_price = min(min_price, p) - return max_profit \ No newline at end of file + return max_profit diff --git a/group-anagrams/i-mprovising.py b/group-anagrams/i-mprovising.py new file mode 100644 index 000000000..2af3ecaf4 --- /dev/null +++ b/group-anagrams/i-mprovising.py @@ -0,0 +1,15 @@ +""" +Time complexity O(n) +Space compexity O(n) + +hash table, sorting +""" + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + group = defaultdict(list) + for s in strs: + sorted_str = str(sorted(s)) + group[sorted_str].append(s) + + return list(group.values()) diff --git a/implement-trie-prefix-tree/i-mprovising.py b/implement-trie-prefix-tree/i-mprovising.py new file mode 100644 index 000000000..dd555e5d1 --- /dev/null +++ b/implement-trie-prefix-tree/i-mprovising.py @@ -0,0 +1,39 @@ +""" +Time complexity O(n) +""" + +class Node: + def __init__(self, end=False): + self.children = {} # hash table + self.end = end + +class Trie: + def __init__(self): + self.root = Node(end=True) + + def insert(self, word: str) -> None: + node = self.root + + for c in word: + if c not in node.children: + node.children[c] = Node() + node = node.children[c] + node.end = True + + def search(self, word: str) -> bool: + node = self.root + for c in word: + if c not in node.children: + return False + node = node.children[c] + if node.end: + return True + return False + + def startsWith(self, prefix: str) -> bool: + node = self.root + for c in prefix: + if c not in node.children: + return False + node = node.children[c] + return True diff --git a/word-break/i-mprovising.py b/word-break/i-mprovising.py new file mode 100644 index 000000000..ebf939215 --- /dev/null +++ b/word-break/i-mprovising.py @@ -0,0 +1,27 @@ +""" +Time complexity O(n*m) n: len(s), m:len(wordDict) +Space compexity O(n) + +dynamic programming +""" + +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + dp = [False for _ in range(len(s))] + for i in range(len(s)): + flag = False + for word in wordDict: + n = len(word) + if i - n + 1 < 0: + continue + if s[i-n+1:i+1] != word: + continue + if i - n + 1 == 0: + flag = True + break + elif i - n + 1 > 0: + if dp[i - n]: + flag = True + break + dp[i] = flag + return dp[-1] From c8051cb2d8a979e86977e17a0f6982d0dc984d84 Mon Sep 17 00:00:00 2001 From: kimseongeun Date: Fri, 2 May 2025 17:14:50 +0900 Subject: [PATCH 3/3] fix group-anagrams time comlexity --- group-anagrams/i-mprovising.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/group-anagrams/i-mprovising.py b/group-anagrams/i-mprovising.py index 2af3ecaf4..6f1d036be 100644 --- a/group-anagrams/i-mprovising.py +++ b/group-anagrams/i-mprovising.py @@ -1,5 +1,9 @@ """ Time complexity O(n) +--> O(n * wlog(w)) +n : 주어지는 단어 개수 +w : 평균 단어 길이 + Space compexity O(n) hash table, sorting