-
-
Notifications
You must be signed in to change notification settings - Fork 195
[jdy8739] week 12 #1049
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
[jdy8739] week 12 #1049
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68f3621
same-tree solution
jdy8739 9fbd3df
Merge branch 'DaleStudy:main' into main
jdy8739 355cfd6
remove-nth-node-from-end-of-list solution
jdy8739 b556897
smae-tree lint error fix
jdy8739 3fae094
non-overlapping-intervals solution
jdy8739 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,32 @@ | ||
/** | ||
* @param {number[][]} intervals | ||
* @return {number} | ||
*/ | ||
var eraseOverlapIntervals = function (intervals) { | ||
|
||
const sort = intervals.sort((a, b) => { | ||
if (a[1] === b[1]) { | ||
return a[0] - b[0]; | ||
} | ||
|
||
return a[1] - b[1]; | ||
}); | ||
|
||
let count = 0; | ||
let max = sort[0][1]; | ||
|
||
for (let i = 0; i < sort.length - 1; i++) { | ||
const right = sort[i + 1][0]; | ||
|
||
if (max > right) { | ||
count++; | ||
} else { | ||
max = sort[i + 1][1]; | ||
} | ||
} | ||
|
||
return count; | ||
}; | ||
|
||
// 시간복잡도 - O(nlogn) 인터벌 뒷 숫자 정렬 시 정렬로 인한 시간복잡도 발생 | ||
// 공간복잡도 - O(1) 특별한 자료구조가 사용되지 않아 상수 공간복잡도 발생 |
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,45 @@ | ||
/** | ||
* 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) { | ||
if (!head) { | ||
return null; | ||
} | ||
|
||
let node = head; | ||
|
||
const list = []; | ||
|
||
while (node) { | ||
list.push(node); | ||
node = node.next; | ||
} | ||
|
||
const targetIndex = list.length - n - 1; | ||
|
||
if (targetIndex === -1 && list.length === 1) { | ||
return null; | ||
} | ||
|
||
if (targetIndex === -1) { | ||
return head.next; | ||
} | ||
|
||
if (list[targetIndex]) { | ||
list[targetIndex].next = list[targetIndex]?.next?.next || null; | ||
} | ||
|
||
return head; | ||
}; | ||
|
||
// 시간복잡도 O(n) - 노드를 한번 순회하여 리스트에 저장 | ||
// 공간복잡도 O(n) - 순회 이후에 제거할 노드의 바로 앞 노드레 접근하기 위하여 모든 노드를 배열에 저장 |
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,48 @@ | ||
/** | ||
* 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 = (a, b) => { | ||
const isValSame = a?.val === b?.val; | ||
const isLeftValSame = a?.left?.val === b?.left?.val; | ||
const isRightValSame = a?.right?.val === b?.right?.val; | ||
|
||
if (!isValSame || !isLeftValSame || !isRightValSame) { | ||
return true; | ||
} | ||
|
||
if (a?.left && b?.left) { | ||
const isLeftDiff = dfs(a.left, b.left); | ||
|
||
if (isLeftDiff) { | ||
return true; | ||
} | ||
} | ||
W | ||
|
||
if (a?.right && b?.right) { | ||
const isRightDiff = dfs(a.right, b.right); | ||
|
||
if (isRightDiff) { | ||
return true; | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
return !dfs(p, q); | ||
}; | ||
|
||
// 시간복잡도 - O(n) p와 q가 같다면 모든 노드를 방문하므로 | ||
// 공간복잡도 - O(h) 깊이우선탐색을 사용하여 트리의 최대 높이만큼 실행환경이 함수호출스택에 저장되므로 |
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 의 특성을 활용한 문제입니다!
잘 활용하시면 공간 복잡도를 O(1) 으로 풀이하실 수 있을거에요.
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.
피드백 감사합니다! 추후에 해당 풀이방식으로 한 번 해결해보겠습니다. :)