-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #579 from Sunjae95/main
[์ ์ฌ] Week13
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @description | ||
* ์ต๋ํ ๋ง์ ์์ ๋์ด๋ผ๋ ๋ฌธ๊ตฌ์์ dynamic programming์ ์ฐ์ | ||
* ์ฐ์๋ ์ง์ ํธ ์ ์๋ค๋ผ๋ ๋ฌธ๊ตฌ์์ ์ ํ์์ ๋์ถ ํ ์ ์์์ | ||
* | ||
* n = length of nums | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var rob = function (nums) { | ||
if (nums.length === 1) return nums[0]; | ||
|
||
const dp = Array(nums.length).fill(0); | ||
|
||
dp[0] = nums[0]; | ||
dp[1] = Math.max(nums[1], dp[0]); | ||
|
||
for (let i = 2; i < nums.length; i++) | ||
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); | ||
|
||
return dp[nums.length - 1]; | ||
}; |
31 changes: 31 additions & 0 deletions
31
lowest-common-ancestor-of-a-binary-search-tree/sunjae95.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @description | ||
* bfs, dfs์ ๊ฐ์ ์ํ ๋ฐฉ๋ฒ๊ณผ treeNode ๊ตฌ์กฐ์ child๊ฐ ์๋ parent๋ผ๋ ์์ฑ์ ๋ถ์ฌํด ๋ถ๋ชจ์ฐพ๊ธฐ๋ฅผ ์์ด๋์ด๋ก ์ ๊ทผ | ||
* ํ์ง๋ง ๋ชจ๋ ๋ ธ๋๋ฅผ ์ํํด์ผํ๊ณ p์ q๊ฐ ์ํ์ง์ ๊ณผ ๋์ด ํฌํจํ๋ ๊ด๊ณ์ธ์ง๋ฅผ ์ค์ ์ผ๋ก ๋ฌธ์ ์ ์ ๊ทผํจ | ||
* ๊ทธ ๊ฒฐ๊ณผ postOrder๋ฅผ ์๊ฐํ๊ฒ ๋์ด ๋ฌธ์ ํ์ด | ||
* | ||
* n = length of total treeNode | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var lowestCommonAncestor = function (root, p, q) { | ||
let answer = null; | ||
|
||
const postOrder = (tree) => { | ||
if (tree === null) return [false, false]; | ||
|
||
const [hasLeftP, hasLeftQ] = postOrder(tree.left); | ||
const [hasRightP, hasRightQ] = postOrder(tree.right); | ||
|
||
const hasP = hasLeftP || hasRightP || tree.val === p.val; | ||
const hasQ = hasLeftQ || hasRightQ || tree.val === q.val; | ||
|
||
if (hasP && hasQ && answer === null) answer = tree; | ||
|
||
return [hasP, hasQ]; | ||
}; | ||
|
||
postOrder(root); | ||
|
||
return answer; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @description | ||
* overlapping์ด ์๋๊ธฐ์ํ ๊ธฐ์ค์ด ํ์ํจ์ ๋๋ | ||
* ์ฒ์์๋ ์์์ , ๋์ ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ์ง๋ง 16๋ฒ ํ ์คํธ์์ ์คํจ | ||
* ์ ๋ ฌ๊ธฐ์ค์ด ๋์ , ์์์ ์์ผ๋ก ์ ๋ ฌํด์ผํ๋ค๊ณ ๊นจ๋ซ๊ฒ ๋จ | ||
* | ||
* n = length of intervals | ||
* time complexity: O(n log n) | ||
* space complexity: O(n) | ||
*/ | ||
var eraseOverlapIntervals = function (intervals) { | ||
intervals.sort((a, b) => { | ||
if (a[1] !== b[1]) return a[1] - b[1]; | ||
if (a[0] !== b[0]) return b[0] - a[0]; | ||
return 0; | ||
}); | ||
|
||
let answer = 0; | ||
const current = intervals[0]; | ||
|
||
for (let i = 1; i < intervals.length; i++) { | ||
const [start, end] = intervals[i]; | ||
|
||
if (current[1] > start) answer++; | ||
else { | ||
current[0] = start; | ||
current[1] = end; | ||
} | ||
} | ||
|
||
return answer; | ||
}; |