Skip to content

[Chaedie] Week 14 #1104

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

Merged
merged 1 commit into from
Mar 16, 2025
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
22 changes: 22 additions & 0 deletions binary-tree-level-order-traversal/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Solution: BFS
Time: O(n)
Space: O(n)
"""

class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root: return []

q = deque([root])
result = []
while q:
level = []
for i in range(len(q)):
node = q.popleft()
level.append(node.val)
if node.left: q.append(node.left)
if node.right: q.append(node.right)
result.append(level)

return result
59 changes: 59 additions & 0 deletions counting-bits/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

"""
Solution:
1) bin() 메서드로 binary 만들어주고
2) 1 의 갯수를 세어준다.
Time: O(n^2)
Space: O(1)
"""

class Solution:
def countBits(self, n: int) -> List[int]:
result = [0 for i in range(n + 1)]

for i in range(n+1):
b = bin(i)
count = 0
for char in b:
if char == '1':
count += 1
result[i] = count

return result

"""
Solution:
1) 2로 나눈 나머지가 1bit 이라는 성질을 이용해서 count
Time: O(n logn)
Space: O(1)
"""

class Solution:
def countBits(self, n: int) -> List[int]:
def count(num):
count = 0
while num > 0:
count += num % 2
num = num // 2
return count

return [count(i) for i in range(n+1)]

"""
Solution:
1) LSB 가 0 1 0 1 반복되므로 num % 2 를 사용한다.
2) 나머지 빗은 LSB를 제외한 값이므로 num // 2 를 사용한다.
Time: O(n)
Space: O(1)
"""

class Solution:

def countBits(self, n: int) -> List[int]:
dp = [0 for i in range(n+1)]

for i in range(1, n+1):
LSB = i % 2
dp[i] = dp[i // 2] + LSB

return dp