-
-
Notifications
You must be signed in to change notification settings - Fork 195
[ganu] Week 12 #1053
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
Merged
Merged
[ganu] Week 12 #1053
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bf5a3c6
feat: 100. Same Tree
gwbaik9717 35459a3
refactor: 100. Same Tree
gwbaik9717 018a2de
feat: 19. Remove Nth Node From End of List
gwbaik9717 44397ef
feat: non-overlapping-intervals
gwbaik9717 dd1ff50
refactor: non-overlapping-intervals
gwbaik9717 77309a4
feat: 297. Serialize and Deserialize Binary Tree
gwbaik9717 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,35 @@ | ||
// Time complexity: O(nlogn) | ||
// Space complexity: O(1) | ||
|
||
/** | ||
* @param {number[][]} intervals | ||
* @return {number} | ||
*/ | ||
var eraseOverlapIntervals = function (intervals) { | ||
intervals.sort((a, b) => { | ||
if (a[0] === b[0]) { | ||
return a[1] - b[1]; | ||
} | ||
|
||
return a[0] - b[0]; | ||
}); | ||
|
||
let count = 0; | ||
let prevEnd = intervals[0][1]; | ||
|
||
for (let i = 1; i < intervals.length; i++) { | ||
const [start, end] = intervals[i]; | ||
|
||
// 구간이 겹칠 때 | ||
if (prevEnd > start) { | ||
count++; | ||
prevEnd = Math.min(prevEnd, end); | ||
continue; | ||
} | ||
|
||
// 구간이 겹치지 않을 때 | ||
prevEnd = end; | ||
} | ||
|
||
return count; | ||
}; |
This file contains hidden or 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,50 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @param {number} n | ||
* @return {ListNode} | ||
*/ | ||
var removeNthFromEnd = function (head, n) { | ||
const reverse = (head) => { | ||
let next = null; | ||
let current = head; | ||
|
||
while (current) { | ||
const temp = current.next; | ||
current.next = next; | ||
next = current; | ||
current = temp; | ||
} | ||
|
||
return next; | ||
}; | ||
|
||
// Reverse | ||
let reversedHead = reverse(head); | ||
|
||
if (n === 1) { | ||
reversedHead = reversedHead.next; | ||
} else { | ||
let prev = null; | ||
let current = reversedHead; | ||
|
||
for (let i = 1; i < n; i++) { | ||
prev = current; | ||
current = current.next; | ||
} | ||
|
||
prev.next = current.next; | ||
} | ||
|
||
// Reverse Again | ||
return reverse(reversedHead); | ||
}; |
This file contains hidden or 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,38 @@ | ||
// n: number of nodes | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* 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} p | ||
* @param {TreeNode} q | ||
* @return {boolean} | ||
*/ | ||
var isSameTree = function (p, q) { | ||
const dfs = (currentP, currentQ) => { | ||
if (!currentP && !currentQ) { | ||
return true; | ||
} | ||
|
||
if ((!currentP && currentQ) || (currentP && !currentQ)) { | ||
return false; | ||
} | ||
|
||
if (currentP.val !== currentQ.val) { | ||
return false; | ||
} | ||
|
||
return ( | ||
dfs(currentP.left, currentQ.left) && dfs(currentP.right, currentQ.right) | ||
); | ||
}; | ||
|
||
return dfs(p, q); | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어려운 문제인데 Queue를 활용하여 잘 풀이해주신것 같습니다. |
This file contains hidden or 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,132 @@ | ||
// n: number of nodes, h: height of tree (max: n) | ||
// Time complexity: O(n) | ||
// Space complexity: O(2^h) | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
|
||
class _Queue { | ||
constructor() { | ||
this.q = []; | ||
this.left = 0; | ||
this.right = 0; | ||
} | ||
|
||
push(value) { | ||
this.q.push(value); | ||
this.right++; | ||
} | ||
|
||
shift() { | ||
const rv = this.q[this.left]; | ||
delete this.q[this.left++]; | ||
|
||
return rv; | ||
} | ||
|
||
isEmpty() { | ||
return this.left === this.right; | ||
} | ||
} | ||
|
||
const isValid = (data) => { | ||
if (data === undefined || data === null) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
/** | ||
* Encodes a tree to a single string. | ||
* | ||
* @param {TreeNode} root | ||
* @return {string} | ||
*/ | ||
var serialize = function (root) { | ||
const answer = [null]; | ||
|
||
const bfs = (current) => { | ||
const q = new _Queue(); | ||
q.push([1, current]); | ||
|
||
while (!q.isEmpty()) { | ||
const [i, current] = q.shift(); | ||
|
||
if (current === null) { | ||
answer[i] = current; | ||
continue; | ||
} | ||
|
||
answer[i] = current.val; | ||
|
||
const left = 2 * i; | ||
const right = left + 1; | ||
|
||
if (current.left) { | ||
q.push([left, current.left]); | ||
} else { | ||
q.push([left, null]); | ||
} | ||
|
||
if (current.right) { | ||
q.push([right, current.right]); | ||
} else { | ||
q.push([right, null]); | ||
} | ||
} | ||
}; | ||
|
||
bfs(root); | ||
|
||
while (answer.length > 1 && !isValid(answer.at(-1))) { | ||
answer.pop(); | ||
} | ||
|
||
return answer; | ||
}; | ||
|
||
/** | ||
* Decodes your encoded data to tree. | ||
* | ||
* @param {string} data | ||
* @return {TreeNode} | ||
*/ | ||
var deserialize = function (data) { | ||
if (data.length === 1) { | ||
return null; | ||
} | ||
|
||
const root = new TreeNode(data[1]); | ||
const q = new _Queue(); | ||
q.push([1, root]); | ||
|
||
while (!q.isEmpty()) { | ||
const [i, current] = q.shift(); | ||
|
||
const left = i * 2; | ||
const right = left + 1; | ||
|
||
if (left <= data.length && isValid(data[left])) { | ||
current.left = new TreeNode(data[left]); | ||
q.push([left, current.left]); | ||
} | ||
|
||
if (right <= data.length && isValid(data[right])) { | ||
current.right = new TreeNode(data[right]); | ||
q.push([right, current.right]); | ||
} | ||
} | ||
|
||
return root; | ||
}; | ||
|
||
/** | ||
* Your functions will be called as such: | ||
* deserialize(serialize(root)); | ||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오.. LinkedList 를 뒤집어서 푸는건 또 참신한 방법이라고 생각되네요.
다른 해답도 참고해 보시면 더욱 시야가 넓어지실것 같습니다!