Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kayden] Week 02 Solutions #355

Merged
merged 5 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str이 문자열인데 어떻게 호출하죠?

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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

range() 함수에 문자열을 넘길 수도 있나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오류가 많았네요.. 다음주에 수정해서 같이 올리겠습니다!

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
Loading