Skip to content

Commit e4ac6b1

Browse files
authored
Merge pull request #579 from Sunjae95/main
[์„ ์žฌ] Week13
2 parents 5ca8ce3 + c014422 commit e4ac6b1

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

โ€Žhouse-robber/sunjae95.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @description
3+
* ์ตœ๋Œ€ํ•œ ๋งŽ์€ ์–‘์˜ ๋ˆ์ด๋ผ๋Š” ๋ฌธ๊ตฌ์—์„œ dynamic programming์„ ์—ฐ์ƒ
4+
* ์—ฐ์†๋œ ์ง‘์€ ํ„ธ ์ˆ˜ ์—†๋‹ค๋ผ๋Š” ๋ฌธ๊ตฌ์—์„œ ์ ํ™”์‹์„ ๋„์ถœ ํ•  ์ˆ˜ ์žˆ์—ˆ์Œ
5+
*
6+
* n = length of nums
7+
* time complexity: O(n)
8+
* space complexity: O(n)
9+
*/
10+
var rob = function (nums) {
11+
if (nums.length === 1) return nums[0];
12+
13+
const dp = Array(nums.length).fill(0);
14+
15+
dp[0] = nums[0];
16+
dp[1] = Math.max(nums[1], dp[0]);
17+
18+
for (let i = 2; i < nums.length; i++)
19+
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
20+
21+
return dp[nums.length - 1];
22+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @description
3+
* bfs, dfs์™€ ๊ฐ™์€ ์ˆœํšŒ ๋ฐฉ๋ฒ•๊ณผ treeNode ๊ตฌ์กฐ์— child๊ฐ€ ์•„๋‹Œ parent๋ผ๋Š” ์†์„ฑ์„ ๋ถ€์—ฌํ•ด ๋ถ€๋ชจ์ฐพ๊ธฐ๋ฅผ ์•„์ด๋””์–ด๋กœ ์ ‘๊ทผ
4+
* ํ•˜์ง€๋งŒ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ˆœํšŒํ•ด์•ผํ•˜๊ณ  p์™€ q๊ฐ€ ์†ํ•œ์ง€์ ๊ณผ ๋‘˜์ด ํฌํ•จํ•˜๋Š” ๊ด€๊ณ„์ธ์ง€๋ฅผ ์ค‘์ ์œผ๋กœ ๋ฌธ์ œ์— ์ ‘๊ทผํ•จ
5+
* ๊ทธ ๊ฒฐ๊ณผ postOrder๋ฅผ ์ƒ๊ฐํ•˜๊ฒŒ ๋˜์–ด ๋ฌธ์ œ ํ’€์ด
6+
*
7+
* n = length of total treeNode
8+
* time complexity: O(n)
9+
* space complexity: O(n)
10+
*/
11+
var lowestCommonAncestor = function (root, p, q) {
12+
let answer = null;
13+
14+
const postOrder = (tree) => {
15+
if (tree === null) return [false, false];
16+
17+
const [hasLeftP, hasLeftQ] = postOrder(tree.left);
18+
const [hasRightP, hasRightQ] = postOrder(tree.right);
19+
20+
const hasP = hasLeftP || hasRightP || tree.val === p.val;
21+
const hasQ = hasLeftQ || hasRightQ || tree.val === q.val;
22+
23+
if (hasP && hasQ && answer === null) answer = tree;
24+
25+
return [hasP, hasQ];
26+
};
27+
28+
postOrder(root);
29+
30+
return answer;
31+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @description
3+
* overlapping์ด ์•ˆ๋˜๊ธฐ์œ„ํ•œ ๊ธฐ์ค€์ด ํ•„์š”ํ•จ์„ ๋А๋‚Œ
4+
* ์ฒ˜์Œ์—๋Š” ์‹œ์ž‘์ , ๋์ ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ–ˆ์ง€๋งŒ 16๋ฒˆ ํ…Œ์ŠคํŠธ์—์„œ ์‹คํŒจ
5+
* ์ •๋ ฌ๊ธฐ์ค€์ด ๋์ , ์‹œ์ž‘์  ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์•ผํ•œ๋‹ค๊ณ  ๊นจ๋‹ซ๊ฒŒ ๋จ
6+
*
7+
* n = length of intervals
8+
* time complexity: O(n log n)
9+
* space complexity: O(n)
10+
*/
11+
var eraseOverlapIntervals = function (intervals) {
12+
intervals.sort((a, b) => {
13+
if (a[1] !== b[1]) return a[1] - b[1];
14+
if (a[0] !== b[0]) return b[0] - a[0];
15+
return 0;
16+
});
17+
18+
let answer = 0;
19+
const current = intervals[0];
20+
21+
for (let i = 1; i < intervals.length; i++) {
22+
const [start, end] = intervals[i];
23+
24+
if (current[1] > start) answer++;
25+
else {
26+
current[0] = start;
27+
current[1] = end;
28+
}
29+
}
30+
31+
return answer;
32+
};

0 commit comments

Comments
ย (0)