Skip to content

Commit

Permalink
Merge pull request #597 from Sunjae95/main
Browse files Browse the repository at this point in the history
[μ„ μž¬] Week14
  • Loading branch information
SamTheKorean authored Nov 17, 2024
2 parents fad730e + 08706ec commit 145a5b0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
31 changes: 31 additions & 0 deletions binary-tree-level-order-traversal/sunjae95.js
Original file line number Diff line number Diff line change
@@ -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;
};
31 changes: 31 additions & 0 deletions house-robber-ii/sunjae95.js
Original file line number Diff line number Diff line change
@@ -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]);
};
18 changes: 18 additions & 0 deletions reverse-bits/sunjae95.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 145a5b0

Please sign in to comment.