From d3dc4eb0caeb6d9e23c04503b1701195c6417b8c Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Sat, 15 Mar 2025 01:07:06 +0900 Subject: [PATCH 1/3] #233 counting-bits solution --- counting-bits/sungjinwi.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 counting-bits/sungjinwi.py diff --git a/counting-bits/sungjinwi.py b/counting-bits/sungjinwi.py new file mode 100644 index 000000000..a30b29e7f --- /dev/null +++ b/counting-bits/sungjinwi.py @@ -0,0 +1,18 @@ +""" + 풀이 : + 가장 낮은 자리의 비트를 제외한 dp의 성분 + 가장 낮은 자리 비트를 통해 dp 구현 + + n의 크기 : N + + TC : O(N) + + SC : O(N) +""" +class Solution: + def countBits(self, n: int) -> List[int]: + dp = [0] * (n + 1) + + for num in range(1, n + 1): + dp[num] = dp[num >> 1] + (num & 1) + + return dp From 1a2e352551ecdd524e0e0d9facf4f3f80e643f59 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Sat, 15 Mar 2025 01:10:49 +0900 Subject: [PATCH 2/3] #250 binary-tree-level-order-traversal solution --- .../sungjinwi.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 binary-tree-level-order-traversal/sungjinwi.py diff --git a/binary-tree-level-order-traversal/sungjinwi.py b/binary-tree-level-order-traversal/sungjinwi.py new file mode 100644 index 000000000..70e1400bd --- /dev/null +++ b/binary-tree-level-order-traversal/sungjinwi.py @@ -0,0 +1,36 @@ +""" + 풀이 : + level을 인자로 함께 넘겨서 level에 해당하는 인덱스에 append + + node 개수 N + + TC : O(N) + 모든 node에 대해 dfs호출 + + SC : O(N) + dfs 함수 호출 스택이 노드 개수 N만큼 +""" + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: + ans = [] + + def dfs(node: Optional[TreeNode], level: int) -> None: + if not node : + return + if len(ans) < level + 1 : + ans.append([node.val]) + else: + ans[level].append(node.val) + dfs(node.left, level + 1) + dfs(node.right, level + 1) + + dfs(root, 0) + + return ans From b5a72860a04bf2cccb97fe531494c9471891f0d4 Mon Sep 17 00:00:00 2001 From: sungjinwi <0202wsj@gmail.com> Date: Sat, 15 Mar 2025 01:12:35 +0900 Subject: [PATCH 3/3] #265 house-robber-ii solution --- house-robber-ii/sungjinwi.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 house-robber-ii/sungjinwi.py diff --git a/house-robber-ii/sungjinwi.py b/house-robber-ii/sungjinwi.py new file mode 100644 index 000000000..45595b1fb --- /dev/null +++ b/house-robber-ii/sungjinwi.py @@ -0,0 +1,22 @@ +""" + 풀이 : + start를 포함하는 경우와 end를 포함하는 경우를 나눠서 dp 진행 + + nums의 갯수 N + + TC : O(N) + + SC : O(1) +""" + +class Solution: + def rob(self, nums: List[int]) -> int: + if len(nums) == 1: + return nums[0] + prv1, cur1 = 0, 0 + prv2, cur2 = 0, 0 + for i in range(1, len(nums)): + prv1, cur1 = cur1, max(cur1, prv1 + nums[i]) + for i in range(len(nums) - 1): + prv2, cur2 = cur2, max(cur2, prv2 + nums[i]) + return max(cur1, cur2)