From 8d083224e7436b6c5897d57c575e5504df95ea65 Mon Sep 17 00:00:00 2001 From: hu6r1s Date: Mon, 25 Aug 2025 21:05:53 +0900 Subject: [PATCH 1/4] feat: Solve valid-parentheses problem --- valid-parentheses/hu6r1s.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 valid-parentheses/hu6r1s.py diff --git a/valid-parentheses/hu6r1s.py b/valid-parentheses/hu6r1s.py new file mode 100644 index 000000000..c4ad22e62 --- /dev/null +++ b/valid-parentheses/hu6r1s.py @@ -0,0 +1,23 @@ +class Solution: + """ + 1. 스택을 활용한 분기 처리 + 여는 괄호일 때는 스택에 무조건 넣어준다. + 닫는 괄호일 때는 대, 중, 소 괄호에 맞춰서 분기를 해줘야 한다. + 스택이 있고, 스택의 마지막이 해당 괄호의 여는 괄호이면 빼내준다. + 이외는 닫힌 괄호가 먼저 나오는 것이기 때문에 False를 반환해준다. + 전형적인 스택 문제로 O(n)의 시간복잡도를 가진다. + """ + def isValid(self, s: str) -> bool: + stack = [] + for word in s: + if word == "(" or word == "{" or word == "[": + stack.append(word) + elif stack and word == ")" and stack[-1] == "(": + stack.pop() + elif stack and word == "]" and stack[-1] == "[": + stack.pop() + elif stack and word == "}" and stack[-1] == "{": + stack.pop() + else: + return False + return True if not stack else False From 7345f01ed5ce77cadaa69efb5c60cd5322fd664b Mon Sep 17 00:00:00 2001 From: hu6r1s Date: Tue, 26 Aug 2025 20:52:59 +0900 Subject: [PATCH 2/4] feat: Solve container-with-most-water problem --- container-with-most-water/hu6r1s.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 container-with-most-water/hu6r1s.py diff --git a/container-with-most-water/hu6r1s.py b/container-with-most-water/hu6r1s.py new file mode 100644 index 000000000..1620fa64b --- /dev/null +++ b/container-with-most-water/hu6r1s.py @@ -0,0 +1,20 @@ + +class Solution: + """ + 1. 브루트포스와 같이 전부 길이를 대조해보고 하면 시간복잡도가 터질 것. + 투포인터 방식을 활용하면 됨. 투포인터에 대한 문제가 코딩테스트로 많이 나올 것 같음. + 확실하게 공부 필요. + """ + def maxArea(self, height: List[int]) -> int: + max_area = 0 + start, end = 0, len(height) - 1 + while start < end: + area = (end - start) * min(height[start], height[end]) + max_area = max(area, max_area) + + if height[start] <= height[end]: + start += 1 + else: + end -= 1 + + return max_area From 16044efe7daa6f280db7fdc6a2a44c0a308e45c5 Mon Sep 17 00:00:00 2001 From: hu6r1s Date: Thu, 28 Aug 2025 21:24:18 +0900 Subject: [PATCH 3/4] feat: Solve design-add-and-search-words-data-structure problem --- .../hu6r1s,py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 design-add-and-search-words-data-structure/hu6r1s,py diff --git a/design-add-and-search-words-data-structure/hu6r1s,py b/design-add-and-search-words-data-structure/hu6r1s,py new file mode 100644 index 000000000..aa6b53cd5 --- /dev/null +++ b/design-add-and-search-words-data-structure/hu6r1s,py @@ -0,0 +1,35 @@ +class WordDictionary: + """ + 알고달레의 영상을 보고 했는데 아직 이해가 잘 되지 않음. + 추가적인 학습 필요 + """ + def __init__(self): + self.root = {"$": True} + + def addWord(self, word: str) -> None: + node = self.root + for ch in word: + if ch not in node: + node[ch] = {"$": False} + node = node[ch] + node["$"] = True + + def search(self, word: str) -> bool: + def dfs(node, idx): + if idx == len(word): + return node["$"] + ch = word[idx] + if ch in node: + return dfs(node[ch], idx + 1) + if ch == ".": + if any(dfs(node[k], idx + 1) for k in node if k != "$"): + return True + return False + + return dfs(self.root, 0) + + +# Your WordDictionary object will be instantiated and called as such: +# obj = WordDictionary() +# obj.addWord(word) +# param_2 = obj.search(word) From ca90209b4d38c649c7b0aa3588111aaf469e5f90 Mon Sep 17 00:00:00 2001 From: hu6r1s Date: Fri, 29 Aug 2025 16:37:02 +0900 Subject: [PATCH 4/4] feat: Solve longest-increasing-subsequence problem --- longest-increasing-subsequence/hu6r1s.py | 40 ++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 longest-increasing-subsequence/hu6r1s.py diff --git a/longest-increasing-subsequence/hu6r1s.py b/longest-increasing-subsequence/hu6r1s.py new file mode 100644 index 000000000..5bc6914a1 --- /dev/null +++ b/longest-increasing-subsequence/hu6r1s.py @@ -0,0 +1,40 @@ +from bisect import bisect_left + +class Solution: + """ + 뭔가 bfs 풀이 방식 + 메모리 초과 + """ + # def lengthOfLIS(self, nums: List[int]) -> int: + # n = len(nums) + # q = deque() + # q.append((-1, float('-inf'), 0)) # (idx, lastValue, length) + # answer = 0 + + # while q: + # idx, last, length = q.popleft() + # answer = max(answer, length) + + # for nxt in range(idx + 1, n): + # if nums[nxt] > last: + # q.append((nxt, nums[nxt], length + 1)) + + # return answer + + # def lengthOfLIS(self, nums: List[int]) -> int: + # dp = [1] * len(nums) + # for cur in range(1, len(nums)): + # for pre in range(cur): + # if nums[pre] < nums[cur]: + # dp[cur] = max(1 + dp[pre], dp[cur]) + # return max(dp) + + def lengthOfLIS(self, nums: List[int]) -> int: + sub = [] + for num in nums: + index = bisect_left(sub, num) + if index == len(sub): + sub.append(num) + else: + sub[index] = num + return len(sub)