diff --git a/best-time-to-buy-and-sell-stock/heypaprika.py b/best-time-to-buy-and-sell-stock/heypaprika.py new file mode 100644 index 000000000..b6e455b48 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/heypaprika.py @@ -0,0 +1,15 @@ +# Big-O 예측 +# Time : O(n) +# Space : O(1) +class Solution: + def maxProfit(self, prices: List[int]) -> int: + min_price = prices[0] + profit = 0 + + for price in prices[1:]: + if min_price > price: + min_price = price + + profit = max(profit, price - min_price) + return profit + diff --git a/encode-and-decode-strings/heypaprika.py b/encode-and-decode-strings/heypaprika.py new file mode 100644 index 000000000..bab6d98d5 --- /dev/null +++ b/encode-and-decode-strings/heypaprika.py @@ -0,0 +1,21 @@ +# Big-O 예측 +# Time : O(n) +# Space : O(1) +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string. + """ + new_str = "-!@$@#!_".join(strs) + return new_str + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings. + """ + return s.split("-!@$@#!_") + + + +strs = ["Hello","World"] +codec = Codec() +codec.decode(codec.encode(strs)) + diff --git a/group-anagrams/heypaprika.py b/group-anagrams/heypaprika.py new file mode 100644 index 000000000..a6f1fe1cf --- /dev/null +++ b/group-anagrams/heypaprika.py @@ -0,0 +1,23 @@ +# Big-O 예측 +# Time : O(k * nlog(n)) (k : strs 배열 수, n : strs 원소의 unique 단어 개수) +# Space : O(k + n) +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + count_dict = {} + num = 0 + dicted_list = [] + for s in strs: + cur = str(sorted(Counter(s).items())) + dicted_list.append(cur) + if cur in count_dict: + continue + count_dict[cur] = num + num += 1 + + ans_list = [[] for _ in range(len(count_dict))] + for k, v in count_dict.items(): + for i, dicted in enumerate(dicted_list): + if dicted == k: + ans_list[v].append(strs[i]) + return ans_list + diff --git a/implement-trie-prefix-tree/heypaprika.py b/implement-trie-prefix-tree/heypaprika.py new file mode 100644 index 000000000..167e92cd2 --- /dev/null +++ b/implement-trie-prefix-tree/heypaprika.py @@ -0,0 +1,37 @@ +# Big-O 예측 +# Time : O(n) (n : 단어 개수) +# Space : O(n) +class Trie: + + def __init__(self): + self.trie_dict = {} + self.trie_dict_list = [{}] + + def insert(self, word: str) -> None: + self.trie_dict[word] = 1 + for i in range(1, len(word) + 1): + if i > len(self.trie_dict_list) and i > 1: + self.trie_dict_list.append({}) + self.trie_dict_list[i-1][word[:i]] = 1 + + def search(self, word: str) -> bool: + return word in self.trie_dict + + def startsWith(self, prefix: str) -> bool: + if len(prefix) > len(self.trie_dict_list): + return False + return prefix in self.trie_dict_list[len(prefix)-1] + + +# Your Trie object will be instantiated and called as such: +trie = Trie() +trie.insert("apple") +trie.search("apple") +trie.search("app") +trie.startsWith("app") +trie.insert("app") +trie.search("app") + +# param_2 = trie.search(word) +# param_3 = trie.startsWith(prefix) + diff --git a/word-break/heypaprika.py b/word-break/heypaprika.py new file mode 100644 index 000000000..fe9c303ef --- /dev/null +++ b/word-break/heypaprika.py @@ -0,0 +1,31 @@ +# Big-O 예측 +# Time : O(n^2) 최악의 경우 wordDict의 개수의 제곱만큼 +# Space : O(n^2) 최악의 경우 a에 wordDict의 개수의 제곱만큼 + +# 쉽게 접근한 풀이 +# 맨 앞의 것을 처리할 수 있는 것을 다 고르기 (startswith) +# 그들을 했을 때의 결과를 가지고 다음 것으로 동일하게 반복 진행하기. + +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + a = [[s]] + i = 1 + s_list = None + duplicate_dict = {} + while True: + s_list = a[-1] + a.append([]) + for s in s_list: + for word in wordDict: + if s.startswith(word): + surplus_word = s[len(word):] + if surplus_word not in duplicate_dict: + a[-1].append(surplus_word) + duplicate_dict[surplus_word] = 1 + # a[-1] = list(set(a[-1])) + if "" in a[-1]: + return True + if not a[-1]: + return False + i += 1 +