Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
heypaprika committed Dec 18, 2024
1 parent fbffa7e commit 88fa0ef
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
30 changes: 30 additions & 0 deletions 3sum/heypaprika.py
Original file line number Diff line number Diff line change
@@ -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)

Original file line number Diff line number Diff line change
@@ -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

20 changes: 20 additions & 0 deletions decode-ways/heypaprika.py
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit 88fa0ef

Please sign in to comment.