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 new file mode 100644 index 000000000..be77336d0 --- /dev/null +++ b/climbing-stairs/jiyseo.py @@ -0,0 +1,15 @@ +class Solution(object): + def climbStairs(self, n): + # 시간복잡도 = 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] + 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/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 + diff --git a/validate-binary-search-tree/jiyseo.py b/validate-binary-search-tree/jiyseo.py new file mode 100644 index 000000000..c5583f3ed --- /dev/null +++ b/validate-binary-search-tree/jiyseo.py @@ -0,0 +1,18 @@ +# 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")) +