diff --git a/best-time-to-buy-and-sell-stock/daiyongg-kim.py b/best-time-to-buy-and-sell-stock/daiyongg-kim.py new file mode 100644 index 000000000..c90793681 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/daiyongg-kim.py @@ -0,0 +1,12 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + min_price = prices[0] + max_profit = 0 + + for price in prices[1:]: + if price < min_price: + min_price = price + elif price - min_price > max_profit: + max_profit = price - min_price + + return max_profit diff --git a/encode-and-decode-strings/daiyongg-kim.py b/encode-and-decode-strings/daiyongg-kim.py new file mode 100644 index 000000000..b7615f0d8 --- /dev/null +++ b/encode-and-decode-strings/daiyongg-kim.py @@ -0,0 +1,40 @@ +class Solution: + """ + @param: strs: a list of strings + @return: encodes a list of strings to a single string. + """ + def encode(self, strs): + # write your code here + result = "" + for word in strs: + word_size = len(word) + result += str(word_size) + "#" + word + + return result + # input ["lint","code","love","you"] + # output "4#lint4#code4#love3#you" + + """ + @param: str: A string + @return: decodes a single string to a list of strings + """ + def decode(self, str): + # write your code here + result = [] + i = 0 + while i < len(str): + j = i + while str[j] != "#": #find the position of '#' + j += 1 + + my_number = int(str[i:j]) + + start = j + 1 # add 1 to skip '#' + end = j + 1 + my_number + + result.append(str[start:end]) + i = end + + return result + # input "4#lint4#code4#love3#you" + # output ["lint","code","love","you"] diff --git a/group-anagrams/daiyongg-kim.py b/group-anagrams/daiyongg-kim.py new file mode 100644 index 000000000..b4509e63c --- /dev/null +++ b/group-anagrams/daiyongg-kim.py @@ -0,0 +1,10 @@ +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + group_anagram = defaultdict(list) + # 초기화: 키가 없으면 자동으로 list() 즉, []를 실행해서 값을 만듦 + + for word in strs: + sorted_word = ''.join(sorted(word)) + group_anagram[sorted_word].append(word) + + return list(group_anagram.values()) diff --git a/implement-trie-prefix-tree/daiyongg-kim.py b/implement-trie-prefix-tree/daiyongg-kim.py new file mode 100644 index 000000000..af236c556 --- /dev/null +++ b/implement-trie-prefix-tree/daiyongg-kim.py @@ -0,0 +1,42 @@ +class Trie: + + def __init__(self): + self.children = {} + self.is_end = False + + def insert(self, word: str) -> None: + node = self + + for c in word: + if c not in node.children: + node.children[c] = Trie() + node = node.children[c] + + node.is_end = True + + + def search(self, word: str) -> bool: + node = self + + for c in word: + if c not in node.children: + return False + node = node.children[c] + + return node.is_end + + def startsWith(self, prefix: str) -> bool: + node = self + for c in prefix: + if c not in node.children: + return False + node = node.children[c] + 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) diff --git a/word-break/daiyongg-kim.py b/word-break/daiyongg-kim.py new file mode 100644 index 000000000..be2b9d041 --- /dev/null +++ b/word-break/daiyongg-kim.py @@ -0,0 +1,25 @@ + +""" Failed Attempt + class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + + for word in wordDict: + if word in s: + s = s.replace(word, '') + else: + return False + return len(s) == 0 +""" +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + word_set = set(wordDict) + + 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 word_set: + dp[i] = True + break + return dp[len(s)]