From abcd4eec8d85720e019ffe72a25dc83801c99996 Mon Sep 17 00:00:00 2001 From: ZetBe Date: Fri, 12 Dec 2025 11:56:40 +0900 Subject: [PATCH 1/5] add solution: best-time-to-buy-and-sell-stock --- best-time-to-buy-and-sell-stock/ZetBe.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/ZetBe.py diff --git a/best-time-to-buy-and-sell-stock/ZetBe.py b/best-time-to-buy-and-sell-stock/ZetBe.py new file mode 100644 index 000000000..5ee14bc28 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/ZetBe.py @@ -0,0 +1,22 @@ +''' +문제: 한번의 구매와 판매로 얻을 수 있는 최대 이익을 구하라. +풀이: 주어진 가격 리스트를 순회하면서 최저가와 최고가를 갱신하며 최대 이익을 계산한다. + 최저가는 현재 최저가격보다 낮은 가격이 나오면 갱신하고, + 최고가는 현재 최고가격보다 높은 가격이 나오면 갱신및 정답인지 비교한다. +시간복잡도: O(n) +공간복잡도: O(1) + +''' + + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + ma, mi = prices[0], prices[0] + answ = 0 + for i in range(1, len(prices)): + if mi > prices[i]: + mi, ma = prices[i], prices[i] + if ma < prices[i]: + ma = prices[i] + answ = max(answ, ma-mi) + return answ \ No newline at end of file From de95a1839a88303b242b0e16475cbdd46b86ee42 Mon Sep 17 00:00:00 2001 From: ZetBe Date: Fri, 12 Dec 2025 11:56:52 +0900 Subject: [PATCH 2/5] add solution: group-anagrams --- group-anagrams/ZetBe.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 group-anagrams/ZetBe.py diff --git a/group-anagrams/ZetBe.py b/group-anagrams/ZetBe.py new file mode 100644 index 000000000..234ebbaea --- /dev/null +++ b/group-anagrams/ZetBe.py @@ -0,0 +1,19 @@ +''' +문제: 주어진 문자열들을 아나그램끼리 그룹화하는 코드를 작성하시오. +풀이: 각 문자열을 정렬하여 동일한 아나그램끼리 같은 키로 묶어 딕셔너리에 저장한 후, 그 값을 반환합니다.(이 때, 해당 문자열을 정렬함으로써 일종의 키 값을 만듭니다.) +시간복잡도: O(n * k log k) (n은 문자열의 개수, k는 각 문자열의 최대 길이) +공간복잡도: O(n) + +''' + + +class Solution: + def groupAnagrams(self, strs: List[str]) -> List[List[str]]: + d = {} + + for i in strs: + k = ''.join(sorted(i)) + if k not in d: + d[k] = [] + d[k].append(i) + return list(d.values()) \ No newline at end of file From 46cbe9f6b0c12555a499bcf510110cb161fcdbe1 Mon Sep 17 00:00:00 2001 From: ZetBe Date: Fri, 12 Dec 2025 11:57:22 +0900 Subject: [PATCH 3/5] add solution: implement-trie-prefix-tree --- implement-trie-prefix-tree/ZetBe.py | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 implement-trie-prefix-tree/ZetBe.py diff --git a/implement-trie-prefix-tree/ZetBe.py b/implement-trie-prefix-tree/ZetBe.py new file mode 100644 index 000000000..81af53727 --- /dev/null +++ b/implement-trie-prefix-tree/ZetBe.py @@ -0,0 +1,33 @@ +''' +문제: 특정 명령이 주어졌을 때, 트라이(Trie) 자료구조를 구현하는 코드를 작성하시오. +풀이: 단순히 문자열을 저장하는 리스트를 사용하여 트라이 자료구조의 기능을 구현합니다. +''' + + +class Trie: + + def __init__(self): + self.arr = [] + + def insert(self, word: str) -> None: + self.arr.append(word) + + def search(self, word: str) -> bool: + for i in self.arr: + if i == word: + return True + return False + + def startsWith(self, prefix: str) -> bool: + n = len(prefix) + for i in self.arr: + if i[:n] == prefix: + return True + return False + + +# 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 From 716304c9423150ee8466b40639ac493a3e24423a Mon Sep 17 00:00:00 2001 From: ZetBe Date: Fri, 12 Dec 2025 11:57:36 +0900 Subject: [PATCH 4/5] add solution: word-break --- word-break/ZetBe.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 word-break/ZetBe.py diff --git a/word-break/ZetBe.py b/word-break/ZetBe.py new file mode 100644 index 000000000..e0481bdb8 --- /dev/null +++ b/word-break/ZetBe.py @@ -0,0 +1,23 @@ +''' +문제: 주어진 문자열이 사전에 있는 단어들로 구성될 수 있는지 여부를 판단하는 코드를 작성하시오. +풀이: 동적 프로그래밍(DP)을 사용하여 문자열의 각 위치까지 사전에 있는 단어들로 구성될 수 있는지 여부를 기록합니다. +시간복잡도: O(n*m) (n은 문자열 s의 길이, m은 단어 사전의 단어 수) +공간복잡도: O(n) +''' + + +class Solution: + def wordBreak(self, s: str, wordDict: List[str]) -> bool: + dp = [False for i in range(len(s))] + for i in wordDict: + if i == s[:len(i)]: + dp[len(i)-1] = True + + for i in range(1, len(s)): + if dp[i-1] == True: + + for j in wordDict: + if i+len(j) <= len(s) and j == s[i:i+len(j)]: + dp[i+len(j)-1] = True + return dp[len(s)-1] + From 3b8d686b73742789a3da5bce108bc5e73e4dcb69 Mon Sep 17 00:00:00 2001 From: ZetBe Date: Fri, 12 Dec 2025 12:03:52 +0900 Subject: [PATCH 5/5] =?UTF-8?q?fix:=20=EC=A4=84=EB=B0=94=EA=BF=88=20?= =?UTF-8?q?=EC=9D=B4=EC=8A=88=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- best-time-to-buy-and-sell-stock/ZetBe.py | 3 ++- group-anagrams/ZetBe.py | 4 +++- implement-trie-prefix-tree/ZetBe.py | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/best-time-to-buy-and-sell-stock/ZetBe.py b/best-time-to-buy-and-sell-stock/ZetBe.py index 5ee14bc28..f91fea2a4 100644 --- a/best-time-to-buy-and-sell-stock/ZetBe.py +++ b/best-time-to-buy-and-sell-stock/ZetBe.py @@ -19,4 +19,5 @@ def maxProfit(self, prices: List[int]) -> int: if ma < prices[i]: ma = prices[i] answ = max(answ, ma-mi) - return answ \ No newline at end of file + return answ + diff --git a/group-anagrams/ZetBe.py b/group-anagrams/ZetBe.py index 234ebbaea..4a3db99b2 100644 --- a/group-anagrams/ZetBe.py +++ b/group-anagrams/ZetBe.py @@ -16,4 +16,6 @@ def groupAnagrams(self, strs: List[str]) -> List[List[str]]: if k not in d: d[k] = [] d[k].append(i) - return list(d.values()) \ No newline at end of file + return list(d.values()) + + diff --git a/implement-trie-prefix-tree/ZetBe.py b/implement-trie-prefix-tree/ZetBe.py index 81af53727..ec250d5e4 100644 --- a/implement-trie-prefix-tree/ZetBe.py +++ b/implement-trie-prefix-tree/ZetBe.py @@ -30,4 +30,5 @@ 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) +