Skip to content

Commit

Permalink
Merge pull request #355 from kjb512/main
Browse files Browse the repository at this point in the history
[kayden] Week 02 Solutions
  • Loading branch information
kjb512 authored Aug 25, 2024
2 parents 849ad6f + 713cfed commit 63b996b
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 시간복잡도: O(N)
# 공간복잡도: O(N)
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right


class Solution:
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:

mapping = {}

for i in range(len(inorder)):
mapping[inorder[i]] = i

preorder = collections.deque(preorder)

def build(start, end):
if start > end: return None

root = TreeNode(preorder.popleft())
mid = mapping[root.val]

root.left = build(start, mid - 1)
root.right = build(mid + 1, end)

return root

return build(0, len(preorder) - 1)
10 changes: 10 additions & 0 deletions counting-bits/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 시간복잡도: O(N)
# 공간복잡도: O(N)
class Solution:

def countBits(self, n: int) -> List[int]:
ans = [0 for _ in range(n + 1)]
for num in range(1, n + 1):
ans[num] = ans[num >> 1] + (num & 1)

return ans
21 changes: 21 additions & 0 deletions decode-ways/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 시간복잡도: O(N)
# 공간복잡도: O(N)
class Solution:
def numDecodings(self, s: str) -> int:
if s[0] == "0":
return 0

n = len(s)
dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1

for i in range(2, n + 1):

if int(s[i - 1]) != 0:
dp[i] += dp[i - 1]

if 10 <= int(s[i - 2:i]) <= 26:
dp[i] += dp[i - 2]

return dp[n]
29 changes: 29 additions & 0 deletions encode-and-decode-strings/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution:

# 시간복잡도: O(N)
# 공간복잡도: O(1)
def encode(self, strs):
res = ""
for str in strs:
size = len(str)
res += str(size)
res += str

return res

# 시간복잡도: O(N)
# 공간복잡도: O(N)
def decode(self, str):
idx = 0
limit = len(str)
res = []

while idx < limit:
num = str[idx]
text = ""
for _ in range(num):
text += str[idx]
idx+=1
res.append(text)

return res
20 changes: 20 additions & 0 deletions valid-anagram/kayden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 시간복잡도: O(N)
# 공간복잡도: O(N)
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False

counter_s = {}
for letter in s:
counter_s[letter] = counter_s.get(letter, 0) + 1

counter_t = {}
for letter in t:
counter_t[letter] = counter_t.get(letter, 0) + 1

for letter, cnt in counter_s.items():
if counter_t.get(letter, 0) != cnt:
return False

return True

0 comments on commit 63b996b

Please sign in to comment.