diff --git a/binary-tree-level-order-traversal/kayden.py b/binary-tree-level-order-traversal/kayden.py new file mode 100644 index 000000000..fb0aa8d9e --- /dev/null +++ b/binary-tree-level-order-traversal/kayden.py @@ -0,0 +1,19 @@ +class Solution: + # 시간복잡도: O(N) + # 공간복잡도: O(N) + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + def traverse(node, level): + if not node: + return + + if len(result) <= level: + result.append([]) + + result[level].append(node.val) + + traverse(node.left, level + 1) + traverse(node.right, level + 1) + + result = [] + traverse(root, 0) + return result diff --git a/house-robber-ii/kayden.py b/house-robber-ii/kayden.py new file mode 100644 index 000000000..4c659a225 --- /dev/null +++ b/house-robber-ii/kayden.py @@ -0,0 +1,14 @@ +class Solution: + # 시간복잡도: O(N) + # 공간복잡도: O(1) + def rob(self, nums: List[int]) -> int: + if len(nums) == 1: + return nums[0] + + def robLinear(houses: List[int]) -> int: + one, two = 0, 0 + for money in houses: + one, two = two, max(two, one + money) + return two + + return max(robLinear(nums[1:]), robLinear(nums[:-1])) diff --git a/reverse-bits/kayden.py b/reverse-bits/kayden.py new file mode 100644 index 000000000..d1d58af83 --- /dev/null +++ b/reverse-bits/kayden.py @@ -0,0 +1,13 @@ +class Solution: + # 시간복잡도: O(1) + # 공간복잡도: O(1) + def reverseBits(self, n: int) -> int: + res = 0 + + for i in range(32): + bit = n & 1 + n = n >> 1 + res = res << 1 + res = res | bit + + return res