From d74dfc161ebd9dacb6a1f430b309709315a2c68d Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Wed, 13 Nov 2024 17:32:44 +0900 Subject: [PATCH 1/3] 1. Reverse Bits --- reverse-bits/sunjae95.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 reverse-bits/sunjae95.js diff --git a/reverse-bits/sunjae95.js b/reverse-bits/sunjae95.js new file mode 100644 index 000000000..93e3f5afb --- /dev/null +++ b/reverse-bits/sunjae95.js @@ -0,0 +1,18 @@ +/** + * @description + * + * n = length of n + * time complexity: O(n) + * space complexity: O(n) + */ +var reverseBits = function (n) { + let answer = 0; + let binary = n.toString(2); + + if (binary.length < 32) binary = "0".repeat(32 - binary.length) + binary; + + for (let i = binary.length - 1; i >= 0; i--) + answer += Math.pow(2, i) * Number(binary[i]); + + return answer; +}; From def6a43c024a6ae5dbbc0a2aa2b74a72322df6f0 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Wed, 13 Nov 2024 17:38:46 +0900 Subject: [PATCH 2/3] 2. Binary Tree Level Order Traversal --- binary-tree-level-order-traversal/sunjae95.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 binary-tree-level-order-traversal/sunjae95.js diff --git a/binary-tree-level-order-traversal/sunjae95.js b/binary-tree-level-order-traversal/sunjae95.js new file mode 100644 index 000000000..7b5136f15 --- /dev/null +++ b/binary-tree-level-order-traversal/sunjae95.js @@ -0,0 +1,31 @@ +/** + * @description + * 동일한 depth를 방문해야하므로 bfs 및 트리 순회 + * + * n = length of node of root + * time complexity: O(n) + * space complexity: O(n) + */ +var levelOrder = function (root) { + if (!root) return []; + + const answer = []; + const queue = [root]; + let queueCurrentIndex = 0; + + while (queue.length > queueCurrentIndex) { + answer.push([]); + const answerLastIndex = answer.length - 1; + const depthEndIndex = queue.length; + + while (depthEndIndex !== queueCurrentIndex) { + const tree = queue[queueCurrentIndex++]; + + answer[answerLastIndex].push(tree.val); + if (tree.left) queue.push(tree.left); + if (tree.right) queue.push(tree.right); + } + } + + return answer; +}; From 08706ec1573fc883cf01dc67fbecd2a92808fe04 Mon Sep 17 00:00:00 2001 From: SeonjaeLee Date: Wed, 13 Nov 2024 17:39:17 +0900 Subject: [PATCH 3/3] 3. House Robber II --- house-robber-ii/sunjae95.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 house-robber-ii/sunjae95.js diff --git a/house-robber-ii/sunjae95.js b/house-robber-ii/sunjae95.js new file mode 100644 index 000000000..468f37fc9 --- /dev/null +++ b/house-robber-ii/sunjae95.js @@ -0,0 +1,31 @@ +/** + * @description + * 점화식: dp[i] = Math.max(dp[i - 1], dp[i - 2] + current); + * 순회한다는 조건이 있으므로 다음과 같이 분기처리 할 수 있다. + * 1. 처음이 선택 O 마지막 X + * 2. 처음이 선택 X 마지막 상관없음 + * + * n = length of nums + * time complexity: O(n) + * space complexity: O(n) + */ +var rob = function (nums) { + if (nums.length === 1) return nums[0]; + if (nums.length === 2) return Math.max(nums[0], nums[1]); + + const hasFirst = Array.from({ length: nums.length }, (_, i) => + i < 2 ? nums[0] : 0 + ); + const noFirst = Array.from({ length: nums.length }, (_, i) => + i === 1 ? nums[i] : 0 + ); + for (let i = 2; i < nums.length; i++) { + hasFirst[i] = Math.max(hasFirst[i - 1], hasFirst[i - 2] + nums[i]); + noFirst[i] = Math.max(noFirst[i - 1], noFirst[i - 2] + nums[i]); + if (i === nums.length - 1) { + hasFirst[i] = Math.max(hasFirst[i - 1], hasFirst[i - 2]); + } + } + + return Math.max(hasFirst[nums.length - 1], noFirst[nums.length - 1]); +};