diff --git a/best-time-to-buy-and-sell-stock/HodaeSsi.py b/best-time-to-buy-and-sell-stock/HodaeSsi.py new file mode 100644 index 000000000..962ef3844 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/HodaeSsi.py @@ -0,0 +1,13 @@ +# 시간 복잡도 : O(n) +# 공간 복잡도 : O(1) +class Solution: + def maxProfit(self, prices: List[int]) -> int: + min_price = float('inf') + max_profit = 0 + + for price in prices: + min_price = min(min_price, price) + max_profit = max(max_profit, price - min_price) + + return max_profit + diff --git a/encode-and-decode-strings/HodaeSsi.py b/encode-and-decode-strings/HodaeSsi.py new file mode 100644 index 000000000..c8cdd9add --- /dev/null +++ b/encode-and-decode-strings/HodaeSsi.py @@ -0,0 +1,22 @@ +# 시간복잡도: O(n) +# 공간복잡도: O(1) + +class Solution: + """ + @param: strs: a list of strings + @return: encodes a list of strings to a single string. + """ + def encode(self, strs): + if not strs: + return "" + return chr(257).join(strs) + + """ + @param: str: A string + @return: decodes a single string to a list of strings + """ + def decode(self, str): + if not str: + return [] + return str.split(chr(257)) + diff --git a/group-anagrams/HodaeSsi.py b/group-anagrams/HodaeSsi.py new file mode 100644 index 000000000..886d8bd4d --- /dev/null +++ b/group-anagrams/HodaeSsi.py @@ -0,0 +1,10 @@ +# 시간복잡도 : O(n*mlogm) (n은 strs의 길이, m은 strs의 원소의 길이) +# 공간복잡도 : O(n) + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + anagrams = collections.defaultdict(list) + for word in strs: + anagrams[''.join(sorted(word))].append(word) + return list(anagrams.values()) + diff --git a/implement-trie-prefix-tree/HodaeSsi.py b/implement-trie-prefix-tree/HodaeSsi.py new file mode 100644 index 000000000..3f599c730 --- /dev/null +++ b/implement-trie-prefix-tree/HodaeSsi.py @@ -0,0 +1,31 @@ +# 시간복잡도: O(n) (n은 문자열의 길이) +# 공간복잡도: O(n) (n은 문자열의 길이) +class Trie: + def __init__(self): + self.root = {} + self.end = False + + def insert(self, word: str) -> None: + node = self.root + for char in word: + if char not in node: + node[char] = {} + node = node[char] + node[self.end] = True + + def search(self, word: str) -> bool: + node = self.root + for char in word: + if char not in node: + return False + node = node[char] + return self.end in node + + def startsWith(self, prefix: str) -> bool: + node = self.root + for char in prefix: + if char not in node: + return False + node = node[char] + return True + diff --git a/word-break/HodaeSsi.py b/word-break/HodaeSsi.py new file mode 100644 index 000000000..526543239 --- /dev/null +++ b/word-break/HodaeSsi.py @@ -0,0 +1,15 @@ +# 시간복잡도 : O(n^2) +# 공간복잡도 : O(n) +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + dp = [False] * (len(s) + 1) + dp[0] = True + + for i in range(1, len(s) + 1): + for j in range(i): + if dp[j] and s[j:i] in wordDict: + dp[i] = True + break + + return dp[-1] +