From 738f82180dfe772838ae4e6ca8cb0c52cb814e85 Mon Sep 17 00:00:00 2001 From: Seongyul Bae Date: Sat, 22 Nov 2025 12:05:50 +0900 Subject: [PATCH 1/9] [week2] start, solved 1 --- product-of-array-except-self/devyulbae.py | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 product-of-array-except-self/devyulbae.py diff --git a/product-of-array-except-self/devyulbae.py b/product-of-array-except-self/devyulbae.py new file mode 100644 index 0000000000..9dee3eef07 --- /dev/null +++ b/product-of-array-except-self/devyulbae.py @@ -0,0 +1,37 @@ +""" +Blind75 - 4. Product of Array Except Self +https://leetcode.com/problems/product-of-array-except-self/ +조건 : +- 분할 없이 O(n) 시간 복잡도 이하로 풀어라 +- nums의 모든 원소의 곱은 32비트 정수 범위 내에 들어온다 +- 나눗셈 금지 + +0이 몇개인가? +- 2개 이상 : 모두 0인 배열 +- 1개 : 0 본인 제외는 0인 배열 +- 0개 : 좌 우 각각의 메모이제이션을 해주면 O(2n)으로 배열 생성, O(n)으로 정답 배열 생성하므로 O(n) +""" +from typing import List + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + zero_count = nums.count(0) + if zero_count > 1: + return [0] * len(nums) + elif zero_count == 1: + all_product = 1 + for num in nums: + all_product *= num if num != 0 else 1 + return [0 if num!= 0 else all_product for num in nums] + + else: + left_product = [nums[0]] + right_product = [nums[-1]] + for i in range(1,len(nums)): + left_product.append(left_product[i-1]*nums[i]) + right_product.append(right_product[i-1]*nums[-1-i]) + + answer = [] + for i in range(len(nums)): + answer.append((left_product[i-1] if i>0 else 1)* (right_product[-1-i] if i < len(nums)-1 else 1)) + return answer \ No newline at end of file From 8fd9d8af623382ff2d5aa7d4e02e102f8ae03b73 Mon Sep 17 00:00:00 2001 From: Seongyul Bae Date: Sat, 22 Nov 2025 12:22:37 +0900 Subject: [PATCH 2/9] solved 2 --- valid-anagram/devulbae.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 valid-anagram/devulbae.py diff --git a/valid-anagram/devulbae.py b/valid-anagram/devulbae.py new file mode 100644 index 0000000000..e69de29bb2 From 44d54ea42ae2bebfaa787e4976f369ad0fbf309b Mon Sep 17 00:00:00 2001 From: Seongyul Bae Date: Sat, 22 Nov 2025 12:54:04 +0900 Subject: [PATCH 3/9] 3 solved --- climbing-stairs/devyulbae.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 climbing-stairs/devyulbae.py diff --git a/climbing-stairs/devyulbae.py b/climbing-stairs/devyulbae.py new file mode 100644 index 0000000000..d64ea5d95f --- /dev/null +++ b/climbing-stairs/devyulbae.py @@ -0,0 +1,24 @@ +""" +Blind 75 - LeetCode Problem 70. Climbing Stairs +https://leetcode.com/problems/climbing-stairs/ + +계단이 n개, 한 번에 1칸 또는 2칸씩 오를 수 있다. +combinations를 썼더니 시간초과 발생 (O(2^n)) + +DP를 이용해 시간복잡도 O(n) 공간복잡도 O(1)로 풀기 +f(n) = f(n-1) + f(n-2) +""" +class Solution: + def climbStairs(self, n: int) -> int: + if n <= 2: + return n + + one_before = 2 # n=2 + two_before = 1 # n=1 + + for _ in range(3, n+1): + current = one_before + two_before # f(1) = f(n-1) + f(n-2) + two_before = one_before # f 밀기 + one_before = current # f 밀기 + + return current \ No newline at end of file From aca532ae0e1031b6c0026d42136704324ca97cca Mon Sep 17 00:00:00 2001 From: Seongyul Bae Date: Sat, 22 Nov 2025 12:57:08 +0900 Subject: [PATCH 4/9] lint error --- climbing-stairs/devyulbae.py | 3 ++- product-of-array-except-self/devyulbae.py | 5 +++-- valid-anagram/devulbae.py | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/climbing-stairs/devyulbae.py b/climbing-stairs/devyulbae.py index d64ea5d95f..a254f3e4fb 100644 --- a/climbing-stairs/devyulbae.py +++ b/climbing-stairs/devyulbae.py @@ -21,4 +21,5 @@ def climbStairs(self, n: int) -> int: two_before = one_before # f 밀기 one_before = current # f 밀기 - return current \ No newline at end of file + return current + diff --git a/product-of-array-except-self/devyulbae.py b/product-of-array-except-self/devyulbae.py index 9dee3eef07..78e1875fde 100644 --- a/product-of-array-except-self/devyulbae.py +++ b/product-of-array-except-self/devyulbae.py @@ -33,5 +33,6 @@ def productExceptSelf(self, nums: List[int]) -> List[int]: answer = [] for i in range(len(nums)): - answer.append((left_product[i-1] if i>0 else 1)* (right_product[-1-i] if i < len(nums)-1 else 1)) - return answer \ No newline at end of file + answer.append((left_product[i-1] if i>0 else 1)* (right_product[-2-i] if i < len(nums)-1 else 1)) + return answer + diff --git a/valid-anagram/devulbae.py b/valid-anagram/devulbae.py index e69de29bb2..7175f0f92b 100644 --- a/valid-anagram/devulbae.py +++ b/valid-anagram/devulbae.py @@ -0,0 +1,15 @@ +""" +Blind 75 - LeetCode Problem 242: Valid Anagram +https://leetcode.com/problems/valid-anagram/ + +t가 s의 애너그림인지 확인하기 +""" +from collections import Counter + +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + if len(s) != len(t): + return False + + return Counter(s) == Counter(t) + From 8b6b96af62f2c95dd2ad99c20d484ce3420c7cfc Mon Sep 17 00:00:00 2001 From: Seongyul Bae Date: Sat, 22 Nov 2025 13:21:04 +0900 Subject: [PATCH 5/9] 4 solved --- 3sum/devyulbae.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 3sum/devyulbae.py diff --git a/3sum/devyulbae.py b/3sum/devyulbae.py new file mode 100644 index 0000000000..a43f85994d --- /dev/null +++ b/3sum/devyulbae.py @@ -0,0 +1,40 @@ +""" +Blind 75 - LeetCode Problem 15. 3Sum +https://leetcode.com/problems/3sum/ +시간복잡도 O(n^2) +""" +from typing import List + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + nums.sort() + answer = [] + n = len(nums) + + for i in range(n): + if i > 0 and nums[i] == nums[i - 1]: + continue # 중복 원소 스킵 + + left, right = i + 1, n - 1 + while left < right: + total = nums[i] + nums[left] + nums[right] + + if total == 0: + answer.append([nums[i], nums[left], nums[right]]) + left += 1 + right -= 1 + + # 중복값 건너뛰기 + while left < right and nums[left] == nums[left - 1]: + left += 1 + while left < right and nums[right] == nums[right + 1]: + right -= 1 + + elif total < 0: + left += 1 # 합이 작으니 left를 키워야 함 + + else: + right -= 1 # 합이 크니 right를 줄여야 함 + + return answer + From 7e78f2a09865ef512b269713e3f92accce1850af Mon Sep 17 00:00:00 2001 From: Seongyul Bae Date: Sat, 22 Nov 2025 13:35:31 +0900 Subject: [PATCH 6/9] =?UTF-8?q?BST=20=EB=AC=B8=EC=A0=9C=EB=8A=94=20GPT=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- validate-binary-search-tree/devyulbae.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 validate-binary-search-tree/devyulbae.py diff --git a/validate-binary-search-tree/devyulbae.py b/validate-binary-search-tree/devyulbae.py new file mode 100644 index 0000000000..1547ca068e --- /dev/null +++ b/validate-binary-search-tree/devyulbae.py @@ -0,0 +1,18 @@ +""" +Blind 75 - LeetCode Problem 98: Validate Binary Search Tree +https://leetcode.com/problems/validate-binary-search-tree/ +이진 탐색 트리인지 확인하기 + +""" +from typing import Optional + +# 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: + \ No newline at end of file From 95f6b0877448126f1c57da19d5bb8a24fb72a3e1 Mon Sep 17 00:00:00 2001 From: Seongyul Bae Date: Sat, 22 Nov 2025 13:36:59 +0900 Subject: [PATCH 7/9] =?UTF-8?q?5=20not=20solved...BST=20=EB=AC=B8=EC=A0=9C?= =?UTF-8?q?=EB=8A=94=20GPT=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- validate-binary-search-tree/devyulbae.py | 27 +++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/validate-binary-search-tree/devyulbae.py b/validate-binary-search-tree/devyulbae.py index 1547ca068e..8943c9eef5 100644 --- a/validate-binary-search-tree/devyulbae.py +++ b/validate-binary-search-tree/devyulbae.py @@ -1,10 +1,11 @@ """ Blind 75 - LeetCode Problem 98: Validate Binary Search Tree https://leetcode.com/problems/validate-binary-search-tree/ -이진 탐색 트리인지 확인하기 +중위 순회 - 왼쪽 서브트리 -> 루트 -> 오른쪽 서브트리 +중위 순회한 값이 오름차순인지 확인하면 된다. """ -from typing import Optional +from typing import Optional, List # Definition for a binary tree node. class TreeNode: @@ -12,7 +13,23 @@ def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right - -class Solution: + +class Solution: # root = [2,1,3] answer = True + def traverse(self, node: Optional[TreeNode], inorder: List[int]) -> None: + if not node: + return + self.traverse(node.left, inorder) + inorder.append(node.val) + self.traverse(node.right, inorder) + + def isValidBST(self, root: Optional[TreeNode]) -> bool: - \ No newline at end of file + inorder = [] + self.traverse(root, inorder) + + for i in range(1, len(inorder)): + if inorder[i] <= inorder[i-1]: + return False + + return True + From fa3dbc3ccd3d0edd5737f007924d3286f17159dd Mon Sep 17 00:00:00 2001 From: Seongyul Bae Date: Sat, 22 Nov 2025 13:41:04 +0900 Subject: [PATCH 8/9] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=EB=AA=85=20=EA=B7=9C?= =?UTF-8?q?=EC=B9=99=20=EA=B2=80=EC=82=AC=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valid-anagram/devulbae.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/valid-anagram/devulbae.py b/valid-anagram/devulbae.py index 7175f0f92b..c68d75c85d 100644 --- a/valid-anagram/devulbae.py +++ b/valid-anagram/devulbae.py @@ -2,7 +2,7 @@ Blind 75 - LeetCode Problem 242: Valid Anagram https://leetcode.com/problems/valid-anagram/ -t가 s의 애너그림인지 확인하기 +t가 s의 애너그램인지 확인하기 """ from collections import Counter @@ -12,4 +12,4 @@ def isAnagram(self, s: str, t: str) -> bool: return False return Counter(s) == Counter(t) - + From ad339752972e987d0b133f3ea272bf39ec97b2b6 Mon Sep 17 00:00:00 2001 From: Seongyul Bae Date: Sat, 22 Nov 2025 13:46:29 +0900 Subject: [PATCH 9/9] =?UTF-8?q?=EC=A7=84=EC=A7=9C=EB=A1=9C=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=EB=AA=85=EC=9D=B4=20=EC=9E=98=EB=AA=BB=EB=90=98?= =?UTF-8?q?=EC=97=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- valid-anagram/{devulbae.py => devyulbae.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename valid-anagram/{devulbae.py => devyulbae.py} (100%) diff --git a/valid-anagram/devulbae.py b/valid-anagram/devyulbae.py similarity index 100% rename from valid-anagram/devulbae.py rename to valid-anagram/devyulbae.py