From af4a449fa243341ac73c102691405f9157d839f4 Mon Sep 17 00:00:00 2001 From: ZetBe Date: Wed, 19 Nov 2025 13:57:47 +0900 Subject: [PATCH 1/6] add solution: valid-anagram --- valid-anagram/ZetBe.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 valid-anagram/ZetBe.py diff --git a/valid-anagram/ZetBe.py b/valid-anagram/ZetBe.py new file mode 100644 index 0000000000..68ca1727a1 --- /dev/null +++ b/valid-anagram/ZetBe.py @@ -0,0 +1,34 @@ +''' +문제: 두 문자열이 주어졌을 때, 두 문자열이 아나그램인지 판별하는 함수를 작성하시오. +아나그램이란, 한 문자열의 문자를 재배열하여 다른 문자열을 만들 수 있는 경우를 말합니다. 예를 들어, "listen"과 "silent"는 아나그램입니다. +해결: 딕테이션을 활용하여 각 문자열의 문자 빈도수를 저장한 후, 두 딕테이션이 동일한지 비교합니다. + +시간복잡도: O(n), n은 문자열의 길이 +각 문자열을 한 번씩 순회하며 딕테이션에 문자의 빈도수를 기록하기 때문에 전체 시간복잡도는 O(n)입니다. +공간복잡도: O(1) +알파벳의 개수는 고정되어 있으므로, 딕테이션에 저장되는 문자의 개수는 최대 26개(영어 알파벳 기준)로 제한됩니다. 따라서 공간복잡도는 O(1)입니다. +''' + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + d1 = {} + d2 = {} + if len(s) != len(t): + return False + + for i in range(len(s)): + if s[i] in d1: + d1[s[i]] += 1 + else: + d1[s[i]] = 1 + + if t[i] in d2: + d2[t[i]] += 1 + else: + d2[t[i]] = 1 + + + if d1 == d2: + return True + return False + From b8907558fd601bec8b495559591f410cb7ccde6d Mon Sep 17 00:00:00 2001 From: ZetBe Date: Wed, 19 Nov 2025 13:58:15 +0900 Subject: [PATCH 2/6] add solution: climbing-stairs --- climbing-stairs/ZetBe.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 climbing-stairs/ZetBe.py diff --git a/climbing-stairs/ZetBe.py b/climbing-stairs/ZetBe.py new file mode 100644 index 0000000000..b823d321a7 --- /dev/null +++ b/climbing-stairs/ZetBe.py @@ -0,0 +1,24 @@ +''' +문제: 계단이 n 계단 있을 때, 한 번에 1계단 또는 2계단씩 오를 수 있는 방법의 수를 구하라. +풀이: 다이나믹 프로그래밍을 이용하여 각 계단에 도달하는 방법의 수를 저장한다. + n번째 계단에 도달하는 방법의 수는 (n-1)번째 계단과 (n-2)번째 계단에 도달하는 방법의 수의 합과 같다. +시간복잡도: O(n) + 각 계단에 도달하는 방법의 수를 한 번씩 계산하므로 전체 시간복잡도는 O(n)이다. +공간복잡도: O(n) + 다이나믹 프로그래밍을 위한 dp 배열이 n 크기를 가지므로 전체 공간복잡도는 O(n)이다. +사용한 자료구조: 리스트 +''' + +class Solution: + def climbStairs(self, n: int) -> int: + dp = [0 for i in range(n+1)] + if n < 3: + return n + dp[1] = 1 + dp[2] = 2 + + for i in range(3, n+1): + dp[i] = dp[i-1]+dp[i-2] + + return dp[n] + From 5db9e872af788e37d14382c0c9c79c1a2b13f5b4 Mon Sep 17 00:00:00 2001 From: ZetBe Date: Wed, 19 Nov 2025 13:59:42 +0900 Subject: [PATCH 3/6] add solution: product-of-array-except-self --- product-of-array-except-self/ZetBe.py | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 product-of-array-except-self/ZetBe.py diff --git a/product-of-array-except-self/ZetBe.py b/product-of-array-except-self/ZetBe.py new file mode 100644 index 0000000000..d963162b51 --- /dev/null +++ b/product-of-array-except-self/ZetBe.py @@ -0,0 +1,40 @@ +''' +문제: 해당 인덱스를 제외한 나머지 인덱스의 곱을 구하라 +풀이: 0의 개수에 따라 경우를 나눠서 풀이 + 1. 0이 2개 이상인 경우: 모든 인덱스의 곱이 0이므로, [0, 0, ..., 0] 반환 + 2. 0이 1개인 경우: 0이 있는 인덱스에는 나머지 인덱스의 곱을, 나머지 인덱스에는 0을 반환 + 3. 0이 없는 경우: 전체 곱을 각 인덱스의 값으로 나누어 반환 +시간복잡도: O(n) + nums 배열을 한 번씩 순회하며 곱을 계산하고, 다시 한 번씩 순회하며 결과를 계산하므로 전체 시간복잡도는 O(n)이다. +공간복잡도: O(n) + 같은 크기의 결과 배열을 반환하므로 전체 공간복잡도는 O(n)이다. +사용한 자료구조: 딕셔너리, 리스트 +''' + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + d = {} + now = 1 + answer = [] + for i in nums: + if i in d: + d[i] += 1 + else: + d[i] = 1 + if i != 0: + now *= i + + if 0 in d and d[0] > 1: + return [0 for i in range(len(nums))] + + if 0 in d and d[0] == 1: + for i in nums: + if i == 0: + answer.append(now) + else: + answer.append(0) + return answer + + for i in nums: + answer.append(now//i) + return answer \ No newline at end of file From 67ed968139c88f66d61558395c8cf64484154faf Mon Sep 17 00:00:00 2001 From: ZetBe Date: Wed, 19 Nov 2025 14:00:14 +0900 Subject: [PATCH 4/6] add solution: 3sum --- 3sum/ZetBe.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 3sum/ZetBe.py diff --git a/3sum/ZetBe.py b/3sum/ZetBe.py new file mode 100644 index 0000000000..d509017f92 --- /dev/null +++ b/3sum/ZetBe.py @@ -0,0 +1,43 @@ +''' +문제: 중복되지 않는 세 수의 조합으로 합이 0이 되는 모든 경우를 찾기 +풀이: 정렬된 배열에서 첫 번째 수를 고정하고, 나머지 두 수를 투 포인터로 탐색 +시간복잡도: O(n^2) + 배열을 정렬하는데 O(n log n), O(n)으로 순회하며 각 수에 대해 투 포인터로 나머지 두 수를 찾는데 O(n)이므로 전체 시간복잡도는 O(n^2)이다. +공간복잡도: O(1) + 추가적인 공간을 사용하지 않으므로 전체 공간복잡도는 O(1)이다. +사용한 자료구조: 리스트 + +별도의 회고: 정답에 중복된 조합이 들어가지 않도록 하기 위해, +첫 번째 수를 고정할 때 이전 수와 같은 경우를 건너뛰고, 나머지 두 수를 찾을 때도 세 수의 합이 0일 경우, 각 두 동일한 수가 연속으로 나오는 경우를 건너뛰도록 하였다. +''' + +import ast +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + answer = [] + for i in range(len(nums)-2): + if i > 0 and nums[i-1] == nums[i]: + continue + l, r = i+1, len(nums)-1 + while l < r: + if nums[i] + nums[l] + nums[r] == 0: + arr = [nums[i], nums[l], nums[r]] + answer.append(arr[:]) + while l < r and nums[l] == nums[l + 1]: + l+=1 + while l < r and nums[r] == nums[r - 1]: + r-=1 + l += 1 + r -= 1 + elif nums[i] + nums[l] + nums[r] < 0: + l += 1 + else: + r -= 1 + + + + + return answer + + From d90267f884a830647a56b43161453d60f46f8c6c Mon Sep 17 00:00:00 2001 From: ZetBe Date: Wed, 19 Nov 2025 14:00:38 +0900 Subject: [PATCH 5/6] add solution: validate-binary-search-tree --- validate-binary-search-tree/ZetBe.py | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 validate-binary-search-tree/ZetBe.py diff --git a/validate-binary-search-tree/ZetBe.py b/validate-binary-search-tree/ZetBe.py new file mode 100644 index 0000000000..aedd61a81d --- /dev/null +++ b/validate-binary-search-tree/ZetBe.py @@ -0,0 +1,31 @@ +''' +문제: 이진 탐색 트리가 유효한지 확인하기 +풀이: 깊이 우선 탐색(DFS)을 사용하여 각 노드가 유효한 범위 내에 있는지 확인 +시간복잡도: O(n) + 모든 노드를 한 번씩 방문하므로 전체 시간복잡도는 O(n)이다. +공간복잡도: O(h) + 재귀 호출 스택이 트리의 높이 h에 비례하므로 전체 공간복잡도는 O(h)이다. +''' + + + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isValidBST(self, root: Optional[TreeNode]) -> bool: + def dfs(root, ma, mi): + if root == None: + return True + + if not (mi < root.val < ma): + return False + + return dfs(root.left, root.val, mi) and dfs(root.right, ma, root.val) + + a = dfs(root, 10**100, -1*(10**100)) + return a + From 988d22380d8abb6cda1a042d4fff6427dbd74393 Mon Sep 17 00:00:00 2001 From: ZetBe Date: Wed, 19 Nov 2025 14:07:30 +0900 Subject: [PATCH 6/6] =?UTF-8?q?fix:=20posix=20=ED=91=9C=EC=A4=80=20?= =?UTF-8?q?=EC=A4=80=EC=88=98=EB=A5=BC=20=EC=9C=84=ED=95=9C=20=EA=B0=9C?= =?UTF-8?q?=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- product-of-array-except-self/ZetBe.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/product-of-array-except-self/ZetBe.py b/product-of-array-except-self/ZetBe.py index d963162b51..0f49928252 100644 --- a/product-of-array-except-self/ZetBe.py +++ b/product-of-array-except-self/ZetBe.py @@ -37,4 +37,5 @@ def productExceptSelf(self, nums: List[int]) -> List[int]: for i in nums: answer.append(now//i) - return answer \ No newline at end of file + return answer +