From 4c4eb212c5cd2e33e3d00095aa36bdbb99f7f9be Mon Sep 17 00:00:00 2001 From: jinah92 Date: Mon, 6 Jan 2025 15:48:58 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20week5=20=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4(221,=20236,=20238)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- best-time-to-buy-and-sell-stock/jinah92.py | 13 ++++++++++++ encode-and-decode-strings/jinah92.py | 24 ++++++++++++++++++++++ group-anagrams/jinah92.py | 24 ++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/jinah92.py create mode 100644 encode-and-decode-strings/jinah92.py create mode 100644 group-anagrams/jinah92.py diff --git a/best-time-to-buy-and-sell-stock/jinah92.py b/best-time-to-buy-and-sell-stock/jinah92.py new file mode 100644 index 000000000..b5c2cf0c9 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/jinah92.py @@ -0,0 +1,13 @@ +# O(N) times, O(1) spaces +# price[i] 가격으로 팔아서 가장 수익을 많이 내려면, i-1번째까지 중 가장 값이 싼 날짜에서 주식을 사면 된다 +class Solution: + def maxProfit(self, prices: List[int]) -> int: + max_profit = 0 + min_price = prices[0] + + for price in prices: + profit = price - min_price + max_profit = max(max_profit, profit) + min_price = min(min_price, price) + + return max_profit diff --git a/encode-and-decode-strings/jinah92.py b/encode-and-decode-strings/jinah92.py new file mode 100644 index 000000000..c97ab01be --- /dev/null +++ b/encode-and-decode-strings/jinah92.py @@ -0,0 +1,24 @@ +# O(N) times, O(1) spaces +class Solution: + """ + @param: strs: a list of strings + @return: encodes a list of strings to a single string. + """ + def encode(self, strs): + encoded_str = "" + for str in strs: + encoded_str += f"{len(str)}:{str}" + return encoded_str + + """ + @param: str: A string + @return: decodes a single string to a list of strings + """ + def decode(self, str): + list_strs, start = [], 0 + while start < len(str): + offset = str.find(":", start) + s_length = int(str[start:offset]) + list_strs.append(str[offset+1:offset+1+s_length]) + start = offset+1+s_length + return list_strs diff --git a/group-anagrams/jinah92.py b/group-anagrams/jinah92.py new file mode 100644 index 000000000..2a93854fc --- /dev/null +++ b/group-anagrams/jinah92.py @@ -0,0 +1,24 @@ +# O(N * NlogN) times, O(N) spaces +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + anagrams_dict = {} + result = [] + + for str in strs: # N 시간 소요 + str_count = {} + for s in str: + if not s in str_count: + str_count[s] = 1 + else: + str_count[s] += 1 + anagrams_keys = [] + for key, val in sorted(str_count.items()): # 최대 N개의 dict items를 배열하므로 NlogN 시간 소요 + anagrams_keys.append(tuple([key, val])) + + anagrams_key = tuple(anagrams_keys) + if tuple(anagrams_keys) not in anagrams_dict: + anagrams_dict[tuple(anagrams_keys)] = [] + anagrams_dict[tuple(anagrams_keys)].append(str) + + + return list(anagrams_dict.values()) From d6d458e3a0b43c16c1594f52efce28d6fe85d92f Mon Sep 17 00:00:00 2001 From: jinah92 Date: Thu, 9 Jan 2025 14:40:10 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20wwek5=20=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20(256)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- implement-trie-prefix-tree/jinah92.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 implement-trie-prefix-tree/jinah92.py diff --git a/implement-trie-prefix-tree/jinah92.py b/implement-trie-prefix-tree/jinah92.py new file mode 100644 index 000000000..e675748e7 --- /dev/null +++ b/implement-trie-prefix-tree/jinah92.py @@ -0,0 +1,16 @@ +# 시간복잡도 +## insert, search는 해시 기반 조회로 인해 O(1), startsWith은 O(n * k) 시간이 소요 (n: 저장된 단어수, k: prefix 길이) +# 공간복잡도 +## O(n * m) 공간이 필요 (n: 저장된 단어 수, m : 각 단어의 평균 길이) +class Trie: + def __init__(self): + self.trie = set() + + def insert(self, word: str) -> None: + self.trie.add(word) + + def search(self, word: str) -> bool: + return word in self.trie + + def startsWith(self, prefix: str) -> bool: + return any(item.startswith(prefix) for item in self.trie) From f5abbbe87e625a0f4003a725906520d7fc062029 Mon Sep 17 00:00:00 2001 From: jinah92 Date: Fri, 10 Jan 2025 13:51:46 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20week5=20=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20(271)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- word-break/jinah92.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 word-break/jinah92.py diff --git a/word-break/jinah92.py b/word-break/jinah92.py new file mode 100644 index 000000000..17765f924 --- /dev/null +++ b/word-break/jinah92.py @@ -0,0 +1,14 @@ +# O(2^s*w) times, O(s) space +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + @cache + 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)