diff --git a/best-time-to-buy-and-sell-stock/thispath98.py b/best-time-to-buy-and-sell-stock/thispath98.py new file mode 100644 index 000000000..dbf3e26eb --- /dev/null +++ b/best-time-to-buy-and-sell-stock/thispath98.py @@ -0,0 +1,12 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + minimum = prices[0] + answer = 0 + for i in range(1, len(prices)): + if minimum > prices[i]: + minimum = prices[i] + else: + diff = prices[i] - minimum + if answer < diff: + answer = diff + return answer diff --git a/encode-and-decode-strings/thispath98.py b/encode-and-decode-strings/thispath98.py new file mode 100644 index 000000000..79998d05f --- /dev/null +++ b/encode-and-decode-strings/thispath98.py @@ -0,0 +1,6 @@ +class Codec: + def encode(self, strs: List[str]) -> str: + return "\n".join(strs) + + def decode(self, s: str) -> List[str]: + return s.split("\n") diff --git a/group-anagrams/thispath98.py b/group-anagrams/thispath98.py new file mode 100644 index 000000000..c4e8241c2 --- /dev/null +++ b/group-anagrams/thispath98.py @@ -0,0 +1,10 @@ +from collections import defaultdict + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + anagram_dict = defaultdict(list) + for string in strs: + anagram_dict[tuple(sorted(string))].append(string) + + answer = list(anagram_dict.values()) + return answer diff --git a/implement-trie-prefix-tree/thispath98.py b/implement-trie-prefix-tree/thispath98.py new file mode 100644 index 000000000..fb81662b7 --- /dev/null +++ b/implement-trie-prefix-tree/thispath98.py @@ -0,0 +1,42 @@ +class Node: + def __init__(self, is_end=False): + self.child = {} + self.is_end = is_end + + +class Trie: + def __init__(self): + self.root = Node() + + def insert(self, word: str) -> None: + node = self.root + for ch in word: + if ch not in node.child: + node.child[ch] = Node() + node = node.child[ch] + node.is_end = True + + def search(self, word: str) -> bool: + node = self.root + for ch in word: + if ch not in node.child: + return False + node = node.child[ch] + + return node.is_end + + def startsWith(self, prefix: str) -> bool: + node = self.root + for ch in prefix: + if ch not in node.child: + return False + node = node.child[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/valid-parentheses/thispath98.py b/valid-parentheses/thispath98.py new file mode 100644 index 000000000..3f445b101 --- /dev/null +++ b/valid-parentheses/thispath98.py @@ -0,0 +1,34 @@ +class Solution: + def isValid(self, s: str) -> bool: + """ + Intuition: + stack 자료구조를 사용해서 닫히는 괄호가 올 경우 + stack의 마지막과 일치하는지 확인한다. + + Time Complexity: + O(N): + 문자열을 한번 스캔하면서 조건문을 확인하므로 + O(N)의 시간복잡도가 소요된다. + + Space Complexity: + O(N): + 최악의 경우 문자열 개수만큼 stack에 저장한다. + """ + stack = [] + for ch in s: + if ch in ["(", "{", "["]: + stack.append(ch) + elif ch in [")", "}", "]"]: + if stack and ( + (ch == ")" and stack[-1] == "(") + or (ch == "}" and stack[-1] == "{") + or (ch == "]" and stack[-1] == "[") + ): + stack.pop() + else: + return False + + if stack: + return False + else: + return True diff --git a/word-break/thispath98.py b/word-break/thispath98.py new file mode 100644 index 000000000..7f188963a --- /dev/null +++ b/word-break/thispath98.py @@ -0,0 +1,20 @@ +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + checked = set() + + def dfs(idx): + if idx in checked: + return + checked.add(idx) + + if idx == len(s): + return True + + for word in wordDict: + word_len = len(word) + if s[idx: idx + word_len] == word: + if dfs(idx + word_len): + return True + return False + + return dfs(0)