From 4d6938933390fcce3306d780800653ff72211cd6 Mon Sep 17 00:00:00 2001 From: jiyseo Date: Thu, 10 Apr 2025 12:02:33 +0900 Subject: [PATCH 1/4] [jiyseo] solve 2 problems --- climbing-stairs/jiyseo.py | 17 +++++++++++++++++ valid-anagram/jiyseo.py | 12 ++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 climbing-stairs/jiyseo.py create mode 100644 valid-anagram/jiyseo.py diff --git a/climbing-stairs/jiyseo.py b/climbing-stairs/jiyseo.py new file mode 100644 index 000000000..dd7425869 --- /dev/null +++ b/climbing-stairs/jiyseo.py @@ -0,0 +1,17 @@ +class Solution(object): + def fac(self, n) : + fac = 1 + for i in range(1, n + 1) : + fac *= i + return fac + + def climbStairs(self, n): + res = 0 + for i in range(n // 2 + 1) : + if i == 0 : + res += 1 + else : + cnt = n - i + res += Solution().fac(cnt)/(Solution().fac(i) * Solution().fac(cnt - i)) + return res + diff --git a/valid-anagram/jiyseo.py b/valid-anagram/jiyseo.py new file mode 100644 index 000000000..fe3ebb32a --- /dev/null +++ b/valid-anagram/jiyseo.py @@ -0,0 +1,12 @@ +class Solution(object): + def isAnagram(self, s, t): + # 시간복잡도 = O(N * (N + M)) + if len(s) != len(t) : + return False + + for i in set(s) : + if s.count(i) != t.count(i) : + return False + + return True + From 39309a04702f9a971491e81c0d194da59a072b05 Mon Sep 17 00:00:00 2001 From: jiyseo Date: Fri, 11 Apr 2025 22:34:45 +0900 Subject: [PATCH 2/4] [jiyseo] solve all problems --- 3sum/jiyseo.py | 43 ++++++++++++++++++++++++++ climbing-stairs/jiyseo.py | 28 ++++++++--------- product-of-array-except-self/jiyseo.py | 18 +++++++++++ validate-binary-search-tree/jiyseo.py | 17 ++++++++++ 4 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 3sum/jiyseo.py create mode 100644 product-of-array-except-self/jiyseo.py create mode 100644 validate-binary-search-tree/jiyseo.py diff --git a/3sum/jiyseo.py b/3sum/jiyseo.py new file mode 100644 index 000000000..779f97b2b --- /dev/null +++ b/3sum/jiyseo.py @@ -0,0 +1,43 @@ +class Solution(object): + def threeSum(self, nums): + # 시간복잡도 = O(N^3) + # length = len(nums) + # result = [] + # for i in range(length - 1) : + # for j in range(i + 1, length) : + # s = - (nums[i] + nums[j]) + # if s in nums[j + 1 : length] : + # result.append(sorted((nums[i], nums[j], s))) + + # return(list(set(map(tuple, result)))) + + # 시간 복잡도 = O(N^2) + nums.sort() + length = len(nums) + result = [] + + for i in range(length - 2) : + if i > 0 and nums[i - 1] == nums[i] : # 같은 숫자인 경우 패스 + continue + target = nums[i] + left = i + 1 + right = length - 1 + + while left < right : + sum = target + nums[left] + nums[right] + if sum > 0 : + right -= 1 + elif sum < 0 : + left += 1 + else : + result.append([target, nums[left], nums[right]]) + # 중복 숫자 건너뛰기 + while left < right and nums[right] == nums[right - 1]: + right -= 1 + while left < right and nums[left] == nums[left + 1]: + left += 1 + right -= 1 + left += 1 + + return result + diff --git a/climbing-stairs/jiyseo.py b/climbing-stairs/jiyseo.py index dd7425869..af91d348c 100644 --- a/climbing-stairs/jiyseo.py +++ b/climbing-stairs/jiyseo.py @@ -1,17 +1,15 @@ class Solution(object): - def fac(self, n) : - fac = 1 - for i in range(1, n + 1) : - fac *= i - return fac - def climbStairs(self, n): - res = 0 - for i in range(n // 2 + 1) : - if i == 0 : - res += 1 - else : - cnt = n - i - res += Solution().fac(cnt)/(Solution().fac(i) * Solution().fac(cnt - i)) - return res - + # 시간복잡도 = O(N) + if n < 2 : + return n + + arr = [0] * (n + 1) + arr[1] = 1 + arr[2] = 2 + + for i in range(3, n + 1) : + arr[i] = arr[i - 1] + arr[i - 2] + + return arr[n] + \ No newline at end of file diff --git a/product-of-array-except-self/jiyseo.py b/product-of-array-except-self/jiyseo.py new file mode 100644 index 000000000..cfa50541c --- /dev/null +++ b/product-of-array-except-self/jiyseo.py @@ -0,0 +1,18 @@ +class Solution(object): + def productExceptSelf(self, nums): + # 시간복잡도 = O(N) + n = 1 + arr = [] + cnt = nums.count(0) + for i in nums : # 0을 제외한 값들의 곱 + if i != 0 : + n *= i + for i in nums : + if i == 0 and cnt == 1: # nums에 0이 한개인 경우 나머지의 곱 + arr.append(n) + elif cnt >= 1 : # nums에 0이 여러개인 경우 무조건 0 + arr.append(0) + else : # 그 외의 경우 + arr.append(n // i) + return(arr) + diff --git a/validate-binary-search-tree/jiyseo.py b/validate-binary-search-tree/jiyseo.py new file mode 100644 index 000000000..ed256a67f --- /dev/null +++ b/validate-binary-search-tree/jiyseo.py @@ -0,0 +1,17 @@ +# Definition for a binary tree node. +# class TreeNode(object): +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution(object): + # 시간복잡도 = O(V + E) + def isValidBST(self, root): + def dfs(node, low, high) : + if not node : + return True + if not (low < node.val < high) : + return False + return dfs(node.left, low, node.val) and dfs(node.right, node.val, high) + + return dfs(root, float("-inf"), float("inf")) From 1bbaffc238a700aa68d675480c8f22affa45e73c Mon Sep 17 00:00:00 2001 From: jiyseo Date: Fri, 11 Apr 2025 22:38:40 +0900 Subject: [PATCH 3/4] add newline --- validate-binary-search-tree/jiyseo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/validate-binary-search-tree/jiyseo.py b/validate-binary-search-tree/jiyseo.py index ed256a67f..c5583f3ed 100644 --- a/validate-binary-search-tree/jiyseo.py +++ b/validate-binary-search-tree/jiyseo.py @@ -15,3 +15,4 @@ def dfs(node, low, high) : return dfs(node.left, low, node.val) and dfs(node.right, node.val, high) return dfs(root, float("-inf"), float("inf")) + From abe1bf1bc9fa5b93e0d8c233ab20cbb1e3d1ae23 Mon Sep 17 00:00:00 2001 From: jiyseo Date: Sat, 12 Apr 2025 01:09:36 +0900 Subject: [PATCH 4/4] add newline --- climbing-stairs/jiyseo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/climbing-stairs/jiyseo.py b/climbing-stairs/jiyseo.py index af91d348c..be77336d0 100644 --- a/climbing-stairs/jiyseo.py +++ b/climbing-stairs/jiyseo.py @@ -12,4 +12,4 @@ def climbStairs(self, n): arr[i] = arr[i - 1] + arr[i - 2] return arr[n] - \ No newline at end of file +