From 6e70c03fa327b22e97e548c1d5a2f871b654b5d6 Mon Sep 17 00:00:00 2001 From: Jinbeom Date: Sat, 16 Nov 2024 19:43:40 +0900 Subject: [PATCH 1/2] Add week 14 solution --- binary-tree-level-order-traversal/kayden.py | 19 +++++++++++++++++++ house-robber-ii/kayden.py | 14 ++++++++++++++ reverse-bits/kayden.py | 13 +++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 binary-tree-level-order-traversal/kayden.py create mode 100644 house-robber-ii/kayden.py create mode 100644 reverse-bits/kayden.py diff --git a/binary-tree-level-order-traversal/kayden.py b/binary-tree-level-order-traversal/kayden.py new file mode 100644 index 000000000..0ab05bc46 --- /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 \ No newline at end of file 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 From 437488cd2b9c9b8b1ac5601be3cbea5043230936 Mon Sep 17 00:00:00 2001 From: Jinbeom Date: Sat, 16 Nov 2024 19:46:22 +0900 Subject: [PATCH 2/2] Fix error --- binary-tree-level-order-traversal/kayden.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/binary-tree-level-order-traversal/kayden.py b/binary-tree-level-order-traversal/kayden.py index 0ab05bc46..fb0aa8d9e 100644 --- a/binary-tree-level-order-traversal/kayden.py +++ b/binary-tree-level-order-traversal/kayden.py @@ -16,4 +16,4 @@ def traverse(node, level): result = [] traverse(root, 0) - return result \ No newline at end of file + return result