diff --git a/3sum/heypaprika.py b/3sum/heypaprika.py new file mode 100644 index 000000000..dadc7eacf --- /dev/null +++ b/3sum/heypaprika.py @@ -0,0 +1,30 @@ +# 2Sum을 활용한 풀이 +# Note : 정렬 후 푸는 것이 더 직관적이고, 빠름 + +""" +복잡도 : 예상 -> 예상한 이유 + +시간 복잡도 : O(n^2) -> nums 배열 2중 for문 +공간 복잡도 : O(n) -> 최악의 경우 nums 배열 길이만큼의 딕셔너리 생성 +""" +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + ans_list = set() + added_pair = {} + for target_i, target_number in enumerate(nums): + cur_ans_list = [] + num_dict = {} + if target_number in added_pair: + continue + + for i in range(target_i + 1, len(nums)): + num_A = nums[i] + num_B = - target_number - num_A + if num_B in num_dict: + cur_ans_list.append(sorted([target_number, num_A, num_B])) + added_pair[target_number] = num_A + num_dict[num_A] = 1 + for item in cur_ans_list: + ans_list.add(tuple(item)) + return list(ans_list) + diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/heypaprika.py b/construct-binary-tree-from-preorder-and-inorder-traversal/heypaprika.py new file mode 100644 index 000000000..675927a0e --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/heypaprika.py @@ -0,0 +1,59 @@ +# 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 + +# preorder -> root, left, right +# inorder -> left, root, right + +# Ex1 [3,9,20,null,null,15,7] +# preorder -> root-1, left, (root-2 - root, left, right) +# inorder -> left, root-1, (root-2 - left, root, right) + +# Ex2 [-1] +# preorder -> root, left(x), right(x) +# inorder -> left(x), root, right(x) + +# Ex3 [3,9,20,11,8,15,7] +# preorder -> root-1, (root-2(left) - root, left, right), (root-3(right) - root, left, right) +# [3,9,11,8,20,15,7] +# inorder -> (root-1(left) - left, root, right), root-2, (root-3(right) - left, root, right) +# [11, 9, 8, 3, 15, 20, 7] + +# Ex4 [3,9,20,11,8] +# preorder -> root-1, (root-2(left) - root, left, right), (root-3(right) - root) +# [3,9,11,8,20] +# inorder -> (root-1(left) - left, root, right), root-2, (root-3(right) - left(X), root) +# [11, 9, 8, 3, 20] + +# 문제풀이 : divide and conquer +# preorder의 첫번째 요소를 가지고 inorder split +# split된 left와 right의 개수에 맞게 preorder을 두 번 째 원소부터 순서대로 할당 +# 예) left 원소 개수 : 3 -> preorder[1:1+3], right 원소 개수 : 2 -> preorder[1+3:] +# left, right 각각 buildTree로 넣기 + +""" +복잡도 : 예상 -> 예상한 이유 + +시간 복잡도 : O(n) -> 분할해서 처리하지만, 모든 노드를 방문하므로 +공간 복잡도 : O(n) -> 입력 배열만큼의 길이와 거의 동일한 배열이 생성되므로 +""" +class Solution: + def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: + if len(preorder) == 0: + return None + if len(preorder) == 1: + return TreeNode(preorder[0]) + root = TreeNode(preorder[0]) + + split_idx = inorder.index(preorder[0]) + left_inorder = inorder[:split_idx] + right_inorder = inorder[split_idx+1:] + left_preorder = preorder[1:len(left_inorder)+1] + right_preorder = preorder[len(left_inorder)+1:] + root.left = self.buildTree(left_preorder, left_inorder) + root.right = self.buildTree(right_preorder, right_inorder) + return root + diff --git a/decode-ways/heypaprika.py b/decode-ways/heypaprika.py new file mode 100644 index 000000000..c850e07c9 --- /dev/null +++ b/decode-ways/heypaprika.py @@ -0,0 +1,20 @@ +""" +복잡도 : 예상 -> 예상한 이유 + +시간 복잡도 : O(n) -> 배열의 길이 만큼 반복하므로 +공간 복잡도 : O(n) -> n+1 길이의 배열 하나를 생성하므로 +""" +class Solution: + def numDecodings(self, s: str) -> int: + if s[0] == '0': + return 0 + a = [0] * (len(s)+1) + a[0] = 1 + a[1] = 1 + for i in range(2, len(s)+1): + if 1 <= int(s[i-1]) <= 9: + a[i] += a[i-1] + if 10 <= int(s[i-2:i]) <= 26: + a[i] += a[i-2] + return a[-1] +