Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[재호] WEEK 14 Solutions #596

Merged
merged 7 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions binary-tree-level-order-traversal/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* 2차
* 각 level의 node 수만큼 끊어서 순회하기
*
* TC: O(N)
* SC: O(N)
* N: total of nodes
*/

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function (root) {
if (!root) {
return [];
}

const result = [];
const queue = [root];

while (queue.length > 0) {
const level = queue.length;
const currentLevelValList = [];

for (let i = 0; i < level; i++) {
const current = queue.shift();

currentLevelValList.push(current.val);

if (current.left) {
queue.push(current.left);
}

if (current.right) {
queue.push(current.right);
}
}

result.push(currentLevelValList);
}

return result;
};

/**
* 1차
* level과 노드를 queue에 추가해서 정답만들기
*
* TC: O(N)
* SC: O(N)
* N: total of nodes
*/

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function (root) {
const result = [];

const queue = [
{
level: 0,
current: root,
},
];

while (queue.length > 0) {
const { level, current } = queue.shift();

if (!current) {
continue;
}

if (result[level]) {
result[level].push(current.val);
} else {
result[level] = [current.val];
}

if (current.left) {
queue.push({
level: level + 1,
current: current.left,
});
}

if (current.right) {
queue.push({
level: level + 1,
current: current.right,
});
}
}

return result;
};
44 changes: 44 additions & 0 deletions house-robber-ii/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* TC: O(N)
* SC; O(1)
*/

/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
if (nums.length < 4) {
return Math.max(...nums);
}

let prevprevprev = nums[0];
let prevprev = nums[1];
let prev = nums[0] + nums[2];

for (let index = 3; index < nums.length - 1; index++) {
const current = Math.max(prevprevprev, prevprev) + nums[index];

prevprevprev = prevprev;
prevprev = prev;
prev = current;
Comment on lines +22 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메모리를 아낄 수 있는 좋은 접근방법인 것 같습니다. 다만 디버깅은 조금 어려운 단점이 있을 것 같습니다.

}

const resultWithoutLast = Math.max(prevprevprev, prevprev, prev);

prevprevprev = nums[1];
prevprev = nums[2];
prev = nums[1] + nums[3];

for (let index = 4; index < nums.length; index++) {
const current = Math.max(prevprevprev, prevprev) + nums[index];

prevprevprev = prevprev;
prevprev = prev;
prev = current;
}

const resultWithoutFirst = Math.max(prevprevprev, prevprev, prev);

return Math.max(resultWithoutLast, resultWithoutFirst);
};
31 changes: 31 additions & 0 deletions house-robber/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* dp[n] = n위치의 집을 훔친다는 가정하에 n위치의 집까지 최대로 훔친 금액
* dp[n] = Math.max(dp[n - 3], dp[n - 2]) + nums[index]
*
* TC: O(N)
* SC: O(1)
*/

/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
if (nums.length < 3) {
return Math.max(...nums);
}

let prevprevprev = nums[0];
let prevprev = nums[1];
let prev = nums[0] + nums[2];

for (let index = 3; index < nums.length; index++) {
const current = Math.max(prevprevprev, prevprev) + nums[index];

prevprevprev = prevprev;
prevprev = prev;
prev = current;
}

return Math.max(prevprevprev, prevprev, prev);
};
32 changes: 32 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* TC: O(logN)
* left와 right 중 한 곳으로만 탐색
*
* SC: O(1)
*/

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/

/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function (root, p, q) {
while (root) {
if (root.val > p.val && root.val > q.val) {
root = root.left;
} else if (root.val < p.val && root.val < q.val) {
root = root.right;
} else {
return root;
}
}
};
27 changes: 27 additions & 0 deletions non-overlapping-intervals/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* TC: O(N * logN)
* 정렬로 인한 시간복잡도
*
* SC: O(1)
*/

/**
* @param {number[][]} intervals
* @return {number}
*/
var eraseOverlapIntervals = function (intervals) {
intervals.sort((a, b) => a[1] - b[1]);

let count = 0;
let lastEnd = Number.MIN_SAFE_INTEGER;

for (const [start, end] of intervals) {
if (start < lastEnd) {
count += 1;
} else {
lastEnd = end;
}
}

return count;
};
15 changes: 15 additions & 0 deletions reverse-bits/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* TC: O(1)
* n 숫자를 32자리 2진수로 변환하고 배열로 변경했을때 최대 길이가 32이므로 상수 복잡도를 갖는다.
*
* SC: O(1)
*/

/**
* @param {number} n - a positive integer
* @return {number} - a positive integer
*/
var reverseBits = function (n) {
const result = n.toString(2).padStart(32, "0").split("").reverse().join("");
return parseInt(result, 2);
};