diff --git a/best-time-to-buy-and-sell-stock/devyejin.py b/best-time-to-buy-and-sell-stock/devyejin.py new file mode 100644 index 0000000000..21f3781b47 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/devyejin.py @@ -0,0 +1,16 @@ +# time complexity : O(n) +# space complexity : O(1) +from typing import List + + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + n = len(prices) + min_price = prices[0] + max_profit = 0 + + for i in range(1, n): + min_price = min(min_price, prices[i]) + max_profit = max(max_profit, prices[i] - min_price) + + return max_profit diff --git a/encode-and-decode-strings/devyejin.py b/encode-and-decode-strings/devyejin.py new file mode 100644 index 0000000000..dcb9bfe09c --- /dev/null +++ b/encode-and-decode-strings/devyejin.py @@ -0,0 +1,33 @@ +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 s in strs: + result += str(len(s)) + "@" + s + return result + + """ + @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] != "@": + j += 1 + # 시작점인 경우 + length = int(str[i:j]) + word = str[j + 1: j + 1 + length] + result.append(word) + i = j + 1 + length + return result diff --git a/implement-trie-prefix-tree/devyejin.py b/implement-trie-prefix-tree/devyejin.py new file mode 100644 index 0000000000..6a335881d4 --- /dev/null +++ b/implement-trie-prefix-tree/devyejin.py @@ -0,0 +1,61 @@ +class TrieNode: + def __init__(self): + self.isEnd = False + self.links = {} + + +class Trie: + + def __init__(self): + self._root = TrieNode() + + def _recurAdd(self, node: TrieNode, word: str) -> None: + if not word: + node.isEnd = True + return + + ch = word[0] + # 부모 노드의 자식에 있는지 확인 + next_link = node.links.get(ch) + + if not next_link: + node.links[ch] = TrieNode() + next_link = node.links[ch] + + self._recurAdd(next_link, word[1:]) + + def insert(self, word: str) -> None: + if not word: + return + + self._recurAdd(self._root, word) + + def _recurSearch(self, node: TrieNode, word: str) -> bool: + if not word: + return node.isEnd + + ch = word[0] + next_link = node.links.get(ch) + if next_link: + return self._recurSearch(next_link, word[1:]) + return False + + def search(self, word: str) -> bool: + if not word: + return False + + return self._recurSearch(self._root, word) + + def startsWith(self, prefix: str) -> bool: + node = self._root + for ch in prefix: + if ch not in node.links: + return False + node = node.links[ch] + 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/devyejin.py b/word-break/devyejin.py new file mode 100644 index 0000000000..26a9ca84a1 --- /dev/null +++ b/word-break/devyejin.py @@ -0,0 +1,24 @@ +from typing import List + + +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + def dfs(subs: str): + if subs in memo: + return memo[subs] + + if not subs: + return True + + for word in wordDict: + if subs.startswith(word): + if dfs(subs[len(word):]): + memo[subs] = True + return True + + memo[subs] = False + return False + + memo = {} + return dfs(s) +