From 905a15a8ca40ed99877546b81bb480105a84454b Mon Sep 17 00:00:00 2001 From: thispath98 Date: Mon, 13 Jan 2025 13:56:00 +0900 Subject: [PATCH 1/3] feat: week 5 commits --- best-time-to-buy-and-sell-stock/thispath98.py | 12 ++++++ encode-and-decode-strings/thispath98.py | 6 +++ group-anagrams/thispath98.py | 10 +++++ implement-trie-prefix-tree/thispath98.py | 42 +++++++++++++++++++ word-break/thispath98.py | 20 +++++++++ 5 files changed, 90 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/thispath98.py create mode 100644 encode-and-decode-strings/thispath98.py create mode 100644 group-anagrams/thispath98.py create mode 100644 implement-trie-prefix-tree/thispath98.py create mode 100644 word-break/thispath98.py 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..f08c8aa66 --- /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) \ No newline at end of file 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) From a33faa099cdee591a4e67ba4c6aa77581948f9c6 Mon Sep 17 00:00:00 2001 From: thispath98 Date: Mon, 13 Jan 2025 14:11:25 +0900 Subject: [PATCH 2/3] feat: Add Valid Parentheses solution --- valid-parentheses/thispath98.py | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 valid-parentheses/thispath98.py 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 From 213e62259e279e2fea605d6687901b08afadc8d4 Mon Sep 17 00:00:00 2001 From: thispath98 Date: Mon, 13 Jan 2025 14:15:03 +0900 Subject: [PATCH 3/3] fix: add whitespace for line lint --- implement-trie-prefix-tree/thispath98.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/implement-trie-prefix-tree/thispath98.py b/implement-trie-prefix-tree/thispath98.py index f08c8aa66..fb81662b7 100644 --- a/implement-trie-prefix-tree/thispath98.py +++ b/implement-trie-prefix-tree/thispath98.py @@ -39,4 +39,4 @@ def startsWith(self, prefix: str) -> bool: # obj = Trie() # obj.insert(word) # param_2 = obj.search(word) -# param_3 = obj.startsWith(prefix) \ No newline at end of file +# param_3 = obj.startsWith(prefix)